#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void bubble(int arr[], int sz, int ou, int ji)
{
int i = 0;
int j = 0;
int k = 0;
int flag = 1;
int tmp = 0;
while (flag)
{
for (i = 0; i < sz; i++)
{
if (arr[i] % 2 == 0 && arr[i + 1] % 2 == 1)
{
tmp = arr[i + 1];
arr[i + 1] = arr[i];
arr[i] = tmp;
}
}
int count = 0;
for (int k = 0; k < ji; k++)
{
if (arr[k] % 2 == 0)
{
break;
}
else
{
count++;
}
}
if (count == ji)
{
flag = 0;
}
}
for (int n = 0; n < sz; n++)
{
printf("%d ", arr[n]);
}
}
int main()
{
int arr[10] = { 0 };
int i = 0;
printf("请输入十个整数:\n");
for (i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
int sz = sizeof(arr) / sizeof(arr[0]);
int num_ou = 0;
int num_ji = 0;
int j = 0;
for (j = 0; j < sz; j++)
{
if (arr[j] % 2 == 0)
{
num_ou++;
}
else
{
num_ji++;
}
}
bubble(arr, sz, num_ou, num_ji);
return 0;
}
【C语言】每日一代码:调整奇数偶数顺序
最新推荐文章于 2024-10-31 16:16:13 发布