更好的办法是,将数组中该位置的值设为一个特殊的数比如-1,为了避免原数组中含有-1的可能,设个计数器 count:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define M 50
int main(void){
int a[M];
int n,i,k;
int j=0;
printf("Enter n:");
scanf("%d",&n);
for(i = 0; i < M;i ++)
a[i] = i;
getchar();
for(count = i = 0; i < M; i ++){
if( a[i] == n ){
count ++;
a[i] = -1;
}
}
printf("print the array:\n");
for (i = 0;i < M; i ++){
if(a[i] == -1 && count)
count --;
else
printf("%d ",a[i]);
}
printf("\n");
system("pause");
return 0;
}
/* 没有测试 */
[此贴子已经被作者于2007-3-19 22:24:50编辑过]