HDU 1263(水果统计 **)

题意是对水果的产地和种类进行统计再按格式输出。

代码如下:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 struct node
 4 {
 5     char name[20],place[20];
 6     int num;
 7 }f[105];
 8 bool cmp(node x,node y)
 9 {
10     if(strcmp(x.place,y.place)<0) return true;
11     if(strcmp(x.place,y.place)==0&&strcmp(x.name,y.name)<0) return true;
12     return false;
13 }
14 int main()
15 {
16     int t,n;
17     scanf("%d",&t);
18     while(t--)
19     {
20         memset(&f,0,sizeof(&f));
21         scanf("%d",&n);
22         for(int i = 0; i < n; ++i)
23         scanf("%s %s %d",f[i].name,f[i].place,&f[i].num);
24         sort(f,f+n,cmp);
25         for(int i = 0; i < n; ++i)
26         {
27             if(strcmp(f[i].place,f[i+1].place)==0)
28             {
29                 if(strcmp(f[i].name,f[i+1].name)==0)
30                 {
31                     f[i+1].num += f[i].num;
32                     f[i].num = 0;//防止同种水果输出多次
33                 }
34             }
35             else
36             {
37                 printf("%s
",f[i].place);
38                 for(int j = 0; j < n; ++j)
39                     if(strcmp(f[i].place,f[j].place)==0&&f[j].num)
40                         printf("   |----%s(%d)
",f[j].name,f[j].num);
41             }
42         }
43         if(t!=0) printf("
");
44     }
45     return 0;
46 }
View Code
日后若能有更好的想法,再来完善。 希望看到的大神不吝赐教 orz
原文地址:https://www.cnblogs.com/Taskr212/p/9562362.html