在做CCF201503-2数字排序的时候,在编译器上正常运行,移交之后显示编译错误。
以下是我写的代码:
#include <stdio.h>
void simple_sort(struct statistic* a, int max);
void tmp(int* a, int* b);
struct statistic {
int num;
int count;
}a[1001];
int main(int argc, const char* argv[])
{
int n, x, max = 0;
scanf("%d", &n);
for (int i = 0; i < 1001; i++) {
a[i].count = 0;
}
for (int i = 0; i < n; i++) {
scanf("%d", &x);
a[x].count++;
a[x].num = x;
if (max < x) max = x;
}
simple_sort(a, max);
for (int i = 0; a[i].count != 0; i++) {
printf("%d %d\n", a[i].num, a[i].count);
}
return 0;
}
void simple_sort(struct statistic* a, int max) {
for (int j = 0; j < max; j++) {
for (int i = 0; i < max; i++) {
if (a[i].count == 0) {
int m;
for (int k = i; k < max; k++) {
a[k].num = a[k + 1].num;
a[k].count = a[k + 1].count;
m = k + 1;
}
a[m].count = 0;
}
if (a[i].count < a[i + 1].count) {
tmp(&a[i].count, &a[i + 1].count);
tmp(&a[i].num, &a[i + 1].num);
}
else if (a[i].count == a[i + 1].count)
if (a[i].num > a[i + 1].num)
tmp(a[i].num, a[i + 1].num);
}
}
return;
}
void tmp(int* a, int* b) {
int tmp = *a;
*a = *b;
*b = tmp;
return;
}
我思路是借助结构数组记录数字和出现次数。
函数simple_sort里的第一个循环是保证数组前面部分没有空的,之后进行排序、输出。
不知道是不是结构数组的问题,怎么还能编译报错呢,我不理解。