输入一个整数数组,实现一个函数,
来调整该数组中数字的顺序使得数组中所有的奇数位于数组的前半部分,
所有偶数位于数组的后半部分。
代码如下
#include<stdio.h>
void Function(int* p, int k) {
int* left = p;
int* right = p + k - 1;
while (left < right) {
while (left < right && *left % 2 != 0) {
left++;
}
while (left < right && *right % 2 == 0) {
right--;
}
int tmp = *left;
*left = *right;
*right = tmp;
}
}
int main()
{
int arr[] = {1,2,3,4,5,6,7,8};
int len = sizeof(arr) / sizeof(arr[0]);
Function(arr,len);
for (int i = 0;i < len;i++) {
printf("%d",*(arr+i));
}
return 0;
}
如果对你有帮助的话就请点个赞吧
标签:arr,right,奇数,int,len,C语言,偶数,数组,left From: https://blog.csdn.net/white16_/article/details/136748072