题目
分析
题目给出列表行数n
,接下来n
行每行给出城市名(第一个单词
)和人名(并没有什么卵用),统计每个城市有几个人头数(ˊ_>ˋ
)并以字典序排序打出。
思路
- 每行取出城市名,将其人头数+1;
qsort()
排序。
代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int cmp(const void* a,const void* b)
{
return strcmp((char*)a, (char*)b);
}
int main(void)
{
int n, i, j, k, c;
char country[2050][80], tmp[80];
scanf("%d", &n);
getchar();
for (i = 0, j = 0; i < n; i++) {
fgets(tmp, 80, stdin);
for (k = 0; tmp[k] != ' '; k++) country[j][k] = tmp[k];
j++;
}
qsort(country, n, sizeof(country[0]), cmp);
for (i = 0, c = 1; i < n; i++)
if (strcmp(country[i], country[i+1]) == 0) {
c++;
} else {
printf("%s %d\n", country[i], c);
c = 1;
}
return 0;
}