#include<stdio.h>
{
//自右向左扫描,将所有非零元素紧凑到右侧
int low,high;
for(low = len-1,high=low ; low>=0;low--)
if(array[low]!=0)
{
array[high] = array[low];
if(low != high) {
array[low] = 0;
}
high -- ;//更新紧凑序列的最左侧元素
}
}
int main()
{
int a[]={1,0,3,0,0,3,2,1};
move(a,8);
for(int i=0;i<8; ++i)
printf("%d\t",a[i]);
return 0;
}
本文介绍了一个C语言程序,该程序能够将数组中所有的零值元素移到数组的左边,而非零值元素则被紧凑地放置在数组的右边。通过这种方式,可以有效地整理数组中的零散数据。

被折叠的 条评论
为什么被折叠?



