stick good
思路:就是按照字典序排列,此题主要是要求掌握pair二元组的用法
1.结构体写法:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <cmath> #include <cstdio> typedef long long ll; #define N 1005 using namespace std; struct point { string weapon; string origin; int harm; } a[700]; int cmp(point x,point y) { if(x.origin!=y.origin) return x.origin<y.origin; if(x.harm!=y.harm) { if(x.harm==1) return 1; if(y.harm==1) return 0; if(y.harm==3) return 1; if(x.harm==3) return 0; } return x.weapon<y.weapon; } int main() { int k,icase=1; while(scanf("%d",&k)==1) { char iharm[50]; for(int i=0; i<k; i++) { cin>>a[i].weapon>>a[i].origin>>iharm; if(iharm[0]=='w') //数字越小优先级越高 a[i].harm=1; else if(iharm[0]=='g') a[i].harm=2; else if(iharm[0]=='s') a[i].harm=3; } sort(a,a+k,cmp); for(int i=0; i<k;) { string str=a[i].origin; cout<<str<<":"<<endl; while(i<k&&str==a[i].origin) { cout<<" "<<a[i].weapon<<" "; if (a[i].harm==3) cout<<"so-so"; else if (a[i].harm==2) cout<<"good"; else cout<<"wonderful"; cout<<endl; ++i; } } } }2.pair写法
#include<iostream> #include<algorithm> #include<cstdio> using namespace std; typedef pair<int ,string>P;//二元组P typedef pair<string,P>Q; //二元组Q Q str[600]; //二元组数组str string le[]={"wonderful","good","so-so"};//定义等级类型 int main() { int n,icase=0,ile; string name,origin,level; while(cin>>n) { icase++; for(int i=0;i<n;i++) { cin>>name>>origin>>level; for( ile=0;ile<3;ile++) { if(level==le[ile]) break; } str[i]=make_pair(origin,P(ile,name)); //将元素压入二元组 } sort(str,str+n);//按照Q.first 比较,如果相同,比较Q.second.first 若相同,比较Q.second.second cout << "Case " << icase << endl; cout << str[0].first << ':' << endl; cout << " " << str[0].second.second <<" " << le[str[0].second.first] << endl; for(int i=1; i<n; i++) { if(str[i].first!=str[i-1].first) cout << str[i].first << ':' << endl; cout << " " << str[i].second.second <<" " << le[str[i].second.first] << endl; } } return 0; } //使用二元组比较方便,如果用结构体得自己编写sort的比较,略微麻烦一点 事例2: 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) 照着题目意思写即可 #include <iostream> #include <cstdio> #include <algorithm> using namespace std; typedef pair<string,string>P; typedef pair<P,int >Q; Q str[300]; int main() { int icase,n; cin>>icase; while(icase--) { cin>>n; string fruit,origin; for(int i=0;i<n;i++) cin>>str[i].first.second>>str[i].first.first>>str[i].second;//水果,地点,数量 sort(str,str+n); cout << str[0].first.first << endl; for(int i=1;i<n;i++) { if(str[i].first.first!=str[i-1].first.first) { cout <<" |----"<< str[i-1].first.second << '(' << str[i-1].second << ')' << endl; cout << str[i].first.first << endl; } else if(str[i].first.second!=str[i-1].first.second) cout <<" |----"<< str[i-1].first.second << '(' << str[i-1].second << ')' << endl; else str[i].second+=str[i-1].second; } cout <<" |----"<< str[n-1].first.second << '(' << str[n-1].second << ')'<<endl; if(icase>0) cout<<endl; } return 0; } pair总结:pair表示的是一个二元组(first,second),仅含有这2个元素,但一般会进行嵌套使用(如上题),并且提供了按照字典序排序的进行大小比较的函数 规则是先比first,如果相同比second,嵌套的话也是同理,但要注意题目数据的处理,谁放在first,谁放在second,需要观察。除了定义pair之外,还可以用make_pair生成pair对象