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\n",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)\n",f[j].name,f[j].num);
41             }
42         }
43         if(t!=0) printf("\n");
44     }
45     return 0;
46 }
View Code

 

转载于:https://www.cnblogs.com/Taskr212/p/9562362.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值