hdu 1263 水果 sort对结构体中字符串二级排序
发布时间:2020-12-13 20:09:38 所属栏目:PHP教程 来源:网络整理
导读:#include stdio.h#include string.h#include algorithmusing namespace std;struct node {char name[90],place[90];int num;}c[105];bool cmp(node x,node y){if(strcmp(x.place,y.place)0) return true;if(strcmp(x.place,y.place)==0strcmp(x.name,y.name)
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
char name[90],place[90];
int num;
}c[105];
bool cmp(node x,node y)
{
if(strcmp(x.place,y.place)<0) return true;
if(strcmp(x.place,y.place)==0&&strcmp(x.name,y.name)<0) return true;
return false;
}
int main()
{
int test,n;
scanf("%d",&test);
while(test--)
{
memset(&c,sizeof(&c));
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%s %s %d",c[i].name,c[i].place,&c[i].num);
sort(c,c+n,cmp);
for(int i=0;i<n;i++){
if(strcmp(c[i].place,c[i+1].place)==0)
{
if(strcmp(c[i].name,c[i+1].name)==0)
c[i+1].num+=c[i].num,c[i].num=0;
}
else
{
printf("%s
",c[i].place);
for(int j=0;j<n;j++)
{
if(strcmp(c[i].place,c[j].place)==0&&c[j].num)
printf(" |----%s(%d)
",c[j].name,c[j].num);
}
}
}
if(test!=0)
printf("
");
}
return 0;
} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |