已知n个整数,将n个整数的奇数放在前,偶数放在后,奇数部分与偶数部分都按升序排列输出。
输入与输出要求:首先输入一个整数n,代表输入整数个数,n不会超过200。然后是n个待排序的整数,每个整数的取值范围是0-1000。输出排序后的n个数,其中奇数在前偶数在后,数与数之间用空格分开,注意第n个数后没有空格而是换行符。
程序运行效果:
Sample 1:
10↙
10 9 8 7 6 5 4 3 2 1↙
1 3 5 7 9 2 4 6 8 10
#include<stdio.h>
int main()
{
int n, a[200], b[200] = { 0 }, c[200] = { 0 }, i, j = 0, k = 0;
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for (i = 0; i < n; i++)
{
if (a[i] % 2 != 0)
{
b[k] = a[i];
k++;
}
else if (a[i] % 2 == 0)
{
c[j] = a[i];
j++;
}
}
//for (i = 0; i < k; i++)
//printf("%d ", b[i]);
//printf('\n');
int count;
int loc;
for (loc = k - 1; loc >= 1; loc--)
{
for (count = 0; count <= loc - 1; count++)
{
if (b[count] > b[count + 1])
{
int temp1;
temp1 = b[count];
b[count] = b[count + 1];
b[count + 1] = temp1;
}
}
}
for (loc = j - 1; loc >= 1; loc--)
{
for (count = 0; count <= loc - 1; count++)
{
if (c[count] > c[count + 1])
{
int temp1;
temp1 = c[count];
c[count] = c[count + 1];
c[count + 1] = temp1;
}
}
}
for (i = 0; i < k; i++)
printf("%d ", b[i]);
for (i = 0; i < j; i++)
printf("%d ", c[i]);
printf("\n");
return 0;
}