将国家读入,用qsort结构体排序,统计每个国家的女人数量,然后输出即可。
注意:
在将国家读入时候,将女人名字忽略,用gets()读入一个临时数组即可,开始用两个scanf("%s",buf)读名字,会Wrong
代码如下:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct infor
{
char country[20];
int cnt;
}text[2000];
int cmp(const void* _a, const void* _b)
{
struct infor* a= (struct infor*)_a;
struct infor* b= (struct infor*)_b;
return strcmp(a->country,b->country);
}
int main()
{
int n,i,j;
char buf[76];
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%s",text[i].country);
text[i].cnt=1;
//scanf("%s",buf);
//scanf("%s",buf);
gets(buf);
}
qsort(text,n,sizeof(text[0]),cmp);
for(i=0;i<n;)
{
for(j=i+1;j<n;j++){
if(strcmp(text[i].country,text[j].country)==0)
text[i].cnt++;
else break;
}
printf("%s %d\n",text[i].country,text[i].cnt);
i=j;
}
return 0;
}