您的位置:首页 > 产品设计 > UI/UE

UVa_10420 - List of Conquests

2014-06-20 20:46 357 查看
将国家读入,用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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: