<分区>
我编写了一个程序,用 0 替换偶数并将它们放在数组的末尾。
Input: 1 2 3 4 5 6 Output: 1 3 5 0 0 0
除了数组之外,约束是只使用一个循环和两个变量。我用 while 循环完成了它,但由于某些未知原因它无限循环。有人可以澄清一下吗?
#include<stdio.h>
#include<stdlib.h>
int main(){
int arr[] = {1,2,3,4,5,6};
int i=0,j=5;
while(i<6){
//Loops till the zero is swapped to the end of the array
if(j!=5){
arr[j] = arr[j] + arr[j+1];
arr[j+1] = arr[j] - arr[j+1];
arr[j] = arr[j] - arr[j+1];
j++; continue;
}
//Checks for even number
if(arr[i]%2==0 && arr[i]!=0){
arr[i]==0;
j=i;
continue;
}
printf("%d ",arr[i]);
i++;
}
return 0;
}