现在他想要一份水果销售情况的明细表,这样就可以很容易掌握所有水果的销售情况了。蒜头君告诉你每一笔销售记录的水果名称,产地和销售的数量,请你帮他生成明细表。
输入格式
第一行是一个整数 N(0 < N \le 1000)N(0<N≤1000),表示工有 NN 次成功的交易。其后有 NN 行数据,每行表示一次交易,由水果名称(小写字母组成,长度不超过 100100),水果产地(小写字母组成,长度不超过 100100)和交易的水果数目(正整数,不超过 10001000)组成.
输出格式
请你输出一份排版格式正确(请分析样本输出)的水果销售情况明细表。这份明细表包括所有水果的产地、名称和销售数目的信息。水果先按产地分类,产地按字母顺序排列;同一产地的水果按照名称排序,名称按字母顺序排序。
样例输入
5 apple shandong 3 pineapple guangdong 1 sugarcane guangdong 1 pineapple guangdong 3 pineapple guangdong 1
样例输出
guangdong |----pineapple(5) |----sugarcane(1) shandong |----apple(3)
#include <iostream>
#include <cstring>
#include <string>
#include <iomanip>
#include <algorithm>
using namespace std;
struct dot
{
string s,t;
int n;
}st[1000];
int cmp(dot x,dot y)
{
if(x.t!=y.t)
return x.t<y.t;
else
return x.s<y.s;
}
int main()
{ int i,j,m,T;
cin>>m;
for(i=0;i<m;i++)
cin>>st[i].s>>st[i].t>>st[i].n;
sort(st,st+m,cmp);
for(i=m-1;i>0;i--)
{
if(st[i].s==st[i-1].s&&st[i].t==st[i-1].t)
st[i-1].n+=st[i].n;
}
cout<<st[0].t<<endl<<" |----"<<st[0].s<<"("<<st[0].n<<")"<<endl;
for(i=1;i<m;i++)
{ if(st[i].t==st[i-1].t&&st[i].s!=st[i-1].s)
cout<<" |----"<<st[i].s<<"("<<st[i].n<<")"<<endl;
else if(st[i].t!=st[i-1].t)
cout<<st[i].t<<endl<<" |----"<<st[i].s<<"("<<st[i].n<<")"<<endl;
}
return 0;
}
学到的点:
2个成对数据当然可以先考虑用map来映射,但要注意map插入时对于已存在的key值插入是当作无效的,而此题好死不死,产地和名称不管哪个当key,都可能遇见重复的。
而且,这里有三个数据来成对,所以,考虑“结构体”。其实感觉结构体这样一设,也不用迭代器了,把题目理解好,就好简单哦!