删除数组相同数字
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int
simplification(
int
a[],
int
n) {
int
i,j,k;
for
(i = 0; i < n - 1; ++i) {
for
(j = i + 1; j < n; ++j) {
if
(a[i] == a[j]) {
for
(k = j; k < n - 1; ++k)
a[k] = a[k + 1];
--j;
--n;
}
}
}
return
n;
}
int
main() {
int
a[30],i,n = 30;
printf
(
"原数组:\n"
);
srand
((unsigned)
time
(NULL));
for
(i = 0; i < n; ++i) {
a[i] =
rand
()%10;
if
(i && i % 10 == 0)
printf
(
"\n"
);
printf
(
"%d "
,a[i]);
}
printf
(
"\n"
);
n = simplification(a,n);
printf
(
"去除相同元素后:\n"
);
for
(i = 0; i < n; ++i) {
if
(i && i % 10 == 0)
printf
(
"\n"
);
printf
(
"%d "
,a[i]);
}
printf
(
"\n"
);
return
0;
}