UVA - 10420 List of Conquests
题目大意。输入多行 以 国家+名字形式的字符串 按字母表顺序输出国家出现的次数
解题思路:将国家单独存放 然后用qsort进行排序后 累加其国家出现次后输出
#include <cstdio>
#include <cstring>
#include<algorithm>
char word[100];
int cmp(const void* _a,const void* _b) {
char* a= (char*)_a;
char* b= (char*)_b;
return strcmp(a,b);
}
int main() {
int flag;
char a[10000][100];
char b[1000];
char c[10000][100];
int d[10000];
char e[100];
int f[10000];
int tttt = 1;
int N;
while (scanf("%d", &N) != EOF ) {
memset(d, 0, sizeof(d));
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
memset(c, 0, sizeof(c));
for (int i = 0; i < N; i++) {
scanf("%s", a[i]);
gets(b);
}
int r = 0;
qsort(a, N, 100, cmp);
strcpy(c[r], a[0]);
for (int i = 0 ; i < N; i++) {
if ( strcmp(c[r], a[i]) == 0 ) {
d[r]++;
f[r] = d[r];
}
else {
r++;
strcpy(c[r], a[i]);
d[r]++;
f[r] = d[r];
}
}
/* for (int i = 0; i < r; i++)
for (int j = 0; j < r - i; j++) {
if (d[j] > d[j+1]) {
int t = d[j];
d[j] = d[j+1];
d[j+1] = t;
strcpy ( e, c[j]);
strcpy ( c[j], c[j+1]);
strcpy ( c[j+1], e);
}
} */
if (tttt != 1) printf("\n");
tttt++;
for (int i = 0; i <= r; i++)
printf("%s %d\n", c[i], d[i]);
}
}