Skip to content

Instantly share code, notes, and snippets.

@novnan
Last active January 1, 2016 10:19
Show Gist options
  • Save novnan/8130835 to your computer and use it in GitHub Desktop.
Save novnan/8130835 to your computer and use it in GitHub Desktop.
/* 有10个整数组成一个数组a,其中整数x出现了若干次,删除多余的x
只保留一个x,显示删除了的x的个数和最后的数组a */
//假如要删除1
#include <stdio.h>
#define N 100
void main()
{
int x, i, count = 0;
int a[10] = {5, 1, 2, 1, 1, 4, 3, 1, 1, 6};
int b[N];
printf("输入要删除的整数:");
scanf("%d", &x);
int position = 0;
int isXSaved = 0;
for(i = 0; i < 10; i++) {
if(a[i] == x) {
count++;
if (isXSaved == 0) {
b[position] = a[i];
position++;
isXSaved = 1;
}
} else {
b[position] = a[i];
position++;
}
}
printf("删除了%d个所要删除的整数\n", count = count - 1);
printf("最后的数组a是:\n");
for (i = 0; i < 10 - count; i++) {
printf("%d ", b[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment