STL专题之map案例

xiaoxiao2021-02-27  295

案例一:sailormoon sort Problem Description As is known to all, long long ago sailormoon once was an association of fighters. Till now, sailormoon is also an association of girls. Owe to some unknown reasons, girls are necessary to fight for peace. Their boss, lcy, wants to strengthen their ability, so he give them his precious collections—weapons for many years. Because these collections are really age-old, it is hard to recognize from one to another. So girls intend to sort them before they use. Each weapon has its name, origin and level of harmfulness ( level contains three ranks: wonderful, good, so-so). In order to make it clear, girls want to sort like this: firstly,sort according to the origin (sort by lexicographic order), if two or more have the same origin, they will be sorted together; secondly, sort according ranks, wonderful is the best, good is next, the third is so-so; thirdly, if two or more have same origin and rank, sort them according to the lexicographic order. Input Input contains multiply cases. Each case contains several lines. First line is an integer N(N<=500)&&(N>0), representing the number of weapons. Then N lines follows. Each line represent a kind of weapon, and contains a set of strings representing name, origin and level of harmfulness. Each string will not exceed 20 characters. Sure that same origin will not exist the same weapon. Output Please output your list after sorting (format according to sample, pay attention to the spaces,ten spaces need ^ ^). Sample Input 5 knife qizhou so-so gun qizhou wonderful knife zhengzhou good stick zhengzhou good rope shengzhou so-so Sample Output Case 1 qizhou: gun wonderful knife so-so shengzhou: rope so-so zhengzhou: knife good stick good 题意 输入格式是:武器类型,产地,评价; 输出要求是你产地为一个集合,输出该产地能生产的武器和对改武器的评价。 输出格式的要求:产地名要字典序排序,评价按”wonderful”、”good”、”so-so”顺序排序。若评价相同,按武器名字典序排序。 备注 map<类型,类型>是一一对应关系。 而map<类型,set<类型> >是一对多关系。 set<类型>是一种集合的定义域或值域。 代码

#include<stdio.h> #include<map> #include<set> #include<iostream> using namespace std; struct Node { string name; string feel; int clas;//"wonderful"is 3;"good" is 2;"so-so" is 1; bool operator <(const Node &x)const { if(clas==x.clas){ return name<x.name; } return clas>x.clas; } }; int main() { int n,cse=0; while(~scanf("%d",&n)) { map<string,set<Node> > x; while(n--) { string name,feel,loca; cin>>name>>loca>>feel; //cout<<loca<<name<<feel<<endl; Node e; e.name=name;e.feel=feel; if(feel=="wonderful")e.clas=3; else if(feel=="good")e.clas=2; else if(feel=="so-so")e.clas=1; x[loca].insert(e); } cout<<"Case "<<++cse<<endl; for(map<string,set<Node> >::iterator it1=x.begin();it1!=x.end();it1++) { cout<<it1->first<<":"<<endl; for(set<Node>::iterator it2=x[it1->first].begin();it2!=x[it1->first].end();it2++) { cout<<" "<<it2->name<<" "<<it2->feel<<endl; } } } return 0; }

案例二:水果 Problem Description 现在他想要一份水果销售情况的明细表,这样Joe就可以很容易掌握所有水果的销售情况了. Input 第一行正整数N(N>0)&&(N<=10)表示有N组测试数据. 每组测试数据的第一行是一个整数M(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<algorithm> #include<map> #include<string.h> #include<stdio.h> using namespace std; struct node { map<string,int> myclas; }; int main() { int T; cin>>T; while(T--) { int n; map<string,node> m; cin>>n; string fruit,place; int cnt; while(n--) { cin>>fruit>>place>>cnt; m[place].myclas[fruit]+=cnt; } for(map<string,node>::iterator it=m.begin();it!=m.end();it++) { cout<<it->first<<endl; for(map<string,int>::iterator it2=it->second.myclas.begin();it2!=it->second.myclas.end();it2++) { cout<<" |----"<<it2->first<<"("<<it2->second<<")"<<endl; } } if(T) { cout<<endl; } } return 0; }
转载请注明原文地址: https://www.6miu.com/read-3027.html

最新回复(0)