题解 | #序列中删除指定数字#
序列中删除指定数字
http://www.nowcoder.com/practice/7bbcdd2177a445a9b66da79512b32dd7
#include <stdio.h> //动态数组来实现该题目
#include <malloc.h>
int main(void)
{
int flag = 0;
int n = 0;
int num = 0;
int j = 0;
scanf("%d", &n);
int* val = (int*)malloc(sizeof(int) * n);
for (int i = 0;i < n;++i) {
scanf("%d", val + i);
}
scanf("%d", &num);
for (int i = 0;i < n;++i) {
if (num == (val + i)) {
flag++;
}
}
int val2 = (int*)malloc(sizeof(int) * (n - flag));
for (int i = 0;i < n;++i) {
if (num != *(val + i)) {
*(val2 + j) = *(val + i);
j++;
}
}
for (int j = 0;j < (n-flag);++j) {
printf("%d ", *(val2 + j));
}
return 0;
}