#include<stdio.h>
int n[10000];
// 我的
int main(void){
int len = 0;
scanf_s("%d", &len);
for (int i = 0; i < len; i++)
scanf_s("%d", &n[i]);
for (int s = 0; s < len; s++)
if (n[s] == 0){
for (int j = s; j < len - 1; j++)
n[j] = n[j + 1];
n[len - 1] = 0;
}
for (int i = 0; i < len; i++)
printf("%d ", n[i]);
return 0;
}
双指针
#include<stdio.h>
int n[10000];
int main(void) {
int s = 0, j = 0, len = 0;
scanf_s("%d", &len);
for (int i = 0; i < len; i++)
scanf_s("%d", &n[i]);
for (s = 0; s < len; s++) {
if (n[s] != 0)
n[j++] = n[s];
}
for (j = j; j < len; j++)
n[j] = 0;
for (int i = 0; i < len; i++)
printf("%d ", n[i]);
return 0;
}