主要是考察Qsort函数的用法。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct s{
char str[22];
}Str;
Str c[110000];
int counts[110000];
int times = 1;
int cmp(const void *a,const void *b){
Str *f1 = (Str*)a;
Str *f2 = (Str*)b;
return strcmp(f1->str,f2->str);
}
int main(){
int n;
int i;
int num;
while(scanf("%d",&n)!=EOF){
for(i=0;i<n;i++) {
scanf("%s",c[i].str);
counts[i] = 0;
}
qsort(c,n,sizeof(c[0]),cmp);
// for(i=0;i<n;i++) printf("%s\n",c[i].str);
i = 1;
num = 1;
while(i<n){
if(strcmp(c[i].str,c[i-1].str)==0){
i++;
num ++;
continue;
}
counts[num]++;
i++;
num=1;
}
counts[num] ++;
printf("Case %d:\n",times++);
for(i=1;i<=n;i++) if(counts[i]>0) printf("%d %d\n",i,counts[i]);
}
return 0;
}