原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1263
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13543 Accepted Submission(s): 5326
#include<iostream> #include<cstdio> #include<map> #include<vector> #include<algorithm> #include<string> using namespace std; struct node{//保存输入的每个产品数据 string a; string b; int c; friend bool operator < (node a,node b) { //指定排序策略,按a排序,如果a相等的话,按b排序,如果b也相等,按c排序 if (a.a!= b.a) return a.a > b.a; else if(a.b!=b.b)return a.b > b.b; else return a.c>b.c; } }; struct node1{//用来保存产地和产品信息,防止同一个产品多次统计 string a; string b; friend bool operator < (node1 a,node1 b) { //指定排序策略,按a排序,如果a相等的话,按b排序 if (a.a!= b.a) return a.a > b.a; else if(a.b!=b.b)return a.b > b.b; } }; bool cmp(node a,node b){//首先按地址排序,如果地址相等按产品排序,如果产品相等,按销售数量排序 if(a.b!=b.b)return a.b<b.b; else if(a.a!=b.a)return a.a<b.a; else return a.c>b.c; } int main(){ int n,m; cin>>n; while(n--){ map<node1,int>mapp; vector<node>vec; node h; node1 h1; cin>>m; int j=1; for(int i=0;i<m;i++){ cin>>h.a>>h.b>>h.c; h1.a=h.a; h1.b=h.b; if(mapp[h1]){ int k=mapp[h1]-1; vec[k].c+=h.c; } else{ vec.push_back(h); mapp[h1]=j++; } } sort(vec.begin(),vec.end(),cmp); string c=vec[0].b; string d=c; cout<<c<<endl; for(int i=0;i<vec.size();i++){ d=vec[i].b; if(d==c){ cout<<" |----"; cout<<vec[i].a<<"("<<vec[i].c<<")"<<endl; } else{ cout<<d<<endl; cout<<" |----"; cout<<vec[i].a<<"("<<vec[i].c<<")"<<endl; c=d; } } if(n)cout<<endl;//最后一组数据后无空行 } }
原文:https://www.cnblogs.com/fromzore/p/10719106.html