# 水果

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 9579    Accepted Submission(s): 3841

Problem Description

Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表,这样Joe就可以很容易掌握所有水果的销售情况了.

Input

Output

Sample Input
15apple shandong 3pineapple guangdong 1sugarcane guangdong 1pineapple guangdong 3pineapple guangdong 1

Sample Output
guangdong |----pineapple(5) |----sugarcane(1)shandong |----apple(3)

Source

Recommend
JGShining

1、结构体

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
char pro[100];
char name[100];
int num;
}unit[120];
int cmp(node x,node y)
{
if(strcmp(x.pro,y.pro)!=0)
return strcmp(x.pro,y.pro)<0;
return strcmp(x.name,y.name)<0;
}
int main ()
{
int i,j,n,t;
cin>>t;
while(t--)
{
cin>>n;
getchar();
for(int i=0;i<n;i++)
{
cin>>unit[i].name>>unit[i].pro>>unit[i].num;
}
sort(unit,unit+n,cmp);
for(int i=0;i<n;i++)
{
for(int j=0;j<i;j++)
{
if((strcmp(unit[j].pro,unit[i].pro)==0)&&(strcmp(unit[j].name,unit[i].name)==0))
{
unit[j].num=unit[j].num+unit[i].num;
break;
}
}
}
/*for(int i=0;i<n;i++)
{
cout<<unit[i].pro<<" "<<unit[i].name<<" "<<unit[i].num<<endl;
}*/
cout<<unit[0].pro<<endl;
cout<<"   |----"<<unit[0].name<<"("<<unit[0].num<<")"<<endl;
for(int i=1;i<n;i++)
{
if((strcmp(unit[i].pro,unit[i-1].pro)==0)&&(strcmp(unit[i].name,unit[i-1].name)==0))
continue;
if(strcmp(unit[i].pro,unit[i-1].pro)==0)
{
cout<<"   |----"<<unit[i].name<<"("<<unit[i].num<<")"<<endl;
}else {
cout<<unit[i].pro<<endl;
cout<<"   |----"<<unit[i].name<<"("<<unit[i].num<<")"<<endl;
}
}
if(t) cout<<endl;
}
return 0;
}


2、map

#include<iostream>
#include<cstdio>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#include<map>
#include<set>
#include<algorithm>
using namespace  std;
int main()
{
//map()可以嵌套再赋值利用
map<string, int> m1;
map<string, map<string, int> > m2;
map<string, int> ::iterator  ite1;
map<string, map<string, int> > ::iterator  ite2;
//freopen("1.txt", "r", stdin);
int t,n,temp;
string str, s;
scanf("%d", &t);
while (t--)
{
m1.clear();  //remember to make map()  be empty
m2.clear();
scanf("%d", &n);
while (n--)
{
cin >> s >> str >> temp;
m2[str][s] += temp;//看清按照什么排序、先pro再name
}

for (ite2 = m2.begin(); ite2 != m2.end(); ite2++)
{
cout<< ite2->first <<endl;
m1 = ite2->second;//把嵌套的map（）复制
for (ite1 = m1.begin(); ite1 != m1.end(); ite1++)
{
printf("   |----%s(%d)\n", ite1->first.c_str(), ite1->second);
//cout << "   |----" << ite1->first << "(" << ite1->second << ")" << endl;
}
}
if (t != 0)
{
printf("\n");
}

}

return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120