hdu 1263 水果 sort对结构体中字符串二级排序

前端之家收集整理的这篇文章主要介绍了hdu 1263 水果 sort对结构体中字符串二级排序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#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; }

猜你在找的PHP相关文章