HDU 1263 Fruit (STL map)

fruit

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

 

Problem Description

Summer is coming ~ ~ good time ah, Oh, all sorts of fruit ~ ~
Joe runs a small fruit shop. He believes that business survival is most welcomed by the customers of the fruit. Now he wants a fruit of sales schedule, so Joe can easily grasp all sales of the fruit.

 

Input

The first line positive integer N (0 <N <= 10 ) expressed N sets of test data.
The first line of each set of test data is an integer M (0 <M <= 100 ), M represents workers have successful transaction Thereafter there are M rows of data, each row represents a transaction, the name of a fruit (lowercase letters, the length of not more than 80), the fruit origin (lowercase letters, the length of not more than 80) the number of fruits and transactions (a positive integer, not more than 100) components.

 

Output

For each set of test data, you export a layout in the correct format (please analyzed sample output) fruit sales list. This list includes the origin, name and number of all fruit sales information. Fruit press Origin classification , origin alphabetical order; fruit of the same origin, names sorted alphabetically sorted by name.
there is a blank line between the two sets of test data is not a blank line after the last set of test data.
 

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)

 

Topic analysis

This question is very well reflected in the advantages of map, we use a two-dimensional map data stored in the first layer is the origin of the fruit and the corresponding second layer is the number of fruits and note that this time will need to traverse the two pointers.

 

Code:

#include<bits/stdc++.h>

using namespace std;

int n,m,i,num;
string  fruit,place;
map<string,map<string,int> > mp;

int main()
{
    cin>>n;
    while(n--)
    {
        mp.clear();
        cin>>m;
        for(i=1;i<=m;i++)
        {
            cin>>fruit>>place>>num;
            mp[place][fruit]+=num;
        }
        map<string,map<string,int> >::iterator p;
        for(p=mp.begin();p!=mp.end();p++)
        {
            cout<<p->first<<endl;
            map<string,int> ::iterator p2;
            for(p2=p->second.begin();p2!=p->second.end();p2++)
            cout<<"   |----"<<p2->first<<'('<<p2->second<<')'<<endl;
        }
        if(n!=0)
        cout<<endl;
    }
 } 

 

Guess you like

Origin www.cnblogs.com/dyhaohaoxuexi/p/11319902.html