hdu 1263 水果 结构的排序+sort自定义排序

水果

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8666    Accepted Submission(s): 3447


Problem Description
夏天来了~~好开心啊,呵呵,好多好多水果~~
Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表,这样Joe就可以很容易掌握所有水果的销售情况了.
 
Input
第一行正整数N(0<N<=10)表示有N组测试数据.
每组测试数据的第一行是一个整数M(0<M<=100),表示工有M次成功的交易.其后有M行数据,每行表示一次交易,由水果名称(小写字母组成,长度不超过80),水果产地(小写字母组成,长度不超过80)和交易的水果数目(正整数,不超过100)组成.
 
Output
对于每一组测试数据,请你输出一份排版格式正确(请分析样本输出)的水果销售情况明细表.这份明细表包括所有水果的产地,名称和销售数目的信息.水果先按产地分类,产地按字母顺序排列;同一产地的水果按照名称排序,名称按字母顺序排序.
两组测试数据之间有一个空行.最后一组测试数据之后没有空行.
 
Sample Input
1 5 apple shandong 3 pineapple guangdong 1 sugarcane guangdong 1 pineapple guangdong 3 pineapple guangdong 1
 
Sample Output
guangdong |----pineapple(5) |----sugarcane(1) shandong |----apple(3)
 
 
 

  解法一:

 
对结构进行排序,使用到了库文件中的sort函数
 
sort给结构排序,有三个参数,sort(a,a+m,cmp)  cmp为自己定义的比较函数,给[a,a+m)排序左闭右开
这个题目的输出格式要注意一下,最后一个测试数据是没有空行的
 
 1 #include <stdlib.h>
 2 #include <stdio.h>
 3 #include <string>
 4 #include <iostream>
 5 #include <map>
 6 #include <algorithm>
 7 using namespace std;
 8 struct lil
 9 {
10    string what,where;
11    int con=0;
12 }li[110];
13 int comp(lil a,lil b)
14 {
15     if(a.where!=b.where)
16         return a.where>b.where;
17     else
18         return a.what>b.what;
19 }
20 int main()
21 {
22     int n;
23     cin>>n;
24         for(int b=0;b<n;b++)
25         {
26            if(b!=0)cout<<endl;
27            int m;
28            cin>>m;
29            for(int i=0;i<m;i++)
30            {
31               cin>>li[i].what;
32               cin>>li[i].where;
33               cin>>li[i].con;
34            }
35            sort(li,li+m,comp);
36            for(int i=0;i<m-1;i++)//合并相同的
37            {
38                if(li[i].what==li[i+1].what&&li[i].where==li[i+1].where)
39                {
40                    li[i+1].con+=li[i].con;
41                    li[i].con=-1;
42                }
43            }
44            cout<<li[m-1].where<<endl;
45            string gg=li[m-1].where;
46            for(int i=m-1;i>=0;i--)
47            {
48                 if(li[i].con==-1)continue;
49                 if(li[i].where==gg)
50                     cout<<"   |----"<<li[i].what<<"("<<li[i].con<<")
";
51                 else
52                 {
53                     cout<<li[i].where<<endl;
54                     gg=li[i].where;
55                     i++;
56                 }
57            }
58        }
59     return 0;
60 }

解法二: 

使用二维map

#include <iostream>  
#include <map>  
#include <string>  
using namespace std;  
int main()  
{  
    int kase;cin>>kase;  
      
    while(kase--){  
        map <string,map<string,int> >cangku;//两个>之间要空格  
        int sum;string name,place;int num;  
        cin>>sum;  
        while(sum--){  
            cin>>name>>place>>num;  
            cangku[place][name]+=num;  
        }  
        map<string,map<string,int> >::iterator it;//两个>之间要空格  
        for(it=cangku.begin();it!=cangku.end();it++){  
            cout<< it->first <<endl;  
  
            map<string,int>::iterator it_2;  
            for(it_2=it->second.begin();it_2!=it->second.end();it_2++){  
                cout << "   |----" << it_2->first << "(" << it_2->second << ")" <<endl;  
            }  
        }  
  
        if(kase!=0)  
            cout<<endl;  
    }  
}  
原文地址:https://www.cnblogs.com/carcar/p/8379932.html