水果
夏天来了~~好开心啊,呵呵,好多好多水果~~
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)
关于简单介绍map的博文:https://blog.csdn.net/qq_42391248/article/details/81041669
题解:最简单想到的就是结构体排序,不过最近在练STL,所以尝试用二重map去写。思路也很简单,就是操作稍微难了点。ps:本题容易忽略一个条件,两组测试数据之间有一个空行.最后一组测试数据之后没有空行.
二重map:定义两个map类型的结构map<string,map<string,int> > a,map<string,int> b很简单,a用于存所以数据,b用于指向a的映照数据。关键在于遍历,用前序遍历,遍历的时候会自动升序排序,还是很方便的。
具体代码如下:
#include<iostream>
#include<queue>
#include<vector>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<cstdlib>
#include<cstdio>
using namespace std;
map<string,map<string,int> > a;
map<string,int> b;
int main()
{
int T;
cin>>T;
while(T--)
{
a.clear();
b.clear();
int t;
cin>>t;
int number;
char apple[20],place[20];
while(t--)
{
scanf("%s%s%d",apple,place,&number);
a[place][apple]+=number;
}
map<string,map<string,int> >::iterator it1;
map<string,int>::iterator it2;
for(it1=a.begin();it1!=a.end();it1++)
{
cout<<it1->first<<endl;
b=it1->second;
for(it2=b.begin();it2!=b.end();it2++)
cout<<" |----"<<it2->first<<"("<<it2->second<<")"<<endl;
}
if(T)
cout<<endl;
}
return 0;
}