HDOJ 1263 水果

来源:互联网 发布:轩辕网络股票今日行情 编辑:程序博客网 时间:2024/05/01 18:58

水果

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


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

Input
第一行正整数N(0<N<=10)表示有N组测试数据.
每组测试数据的第一行是一个整数M(0<M<=100),表示工有M次成功的交易.其后有M行数据,每行表示一次交易,由水果名称(小写字母组成,长度不超过80),水果产地(小写字母组成,长度不超过80)和交易的水果数目(正整数,不超过100)组成.
 

Output
对于每一组测试数据,请你输出一份排版格式正确(请分析样本输出)的水果销售情况明细表.这份明细表包括所有水果的产地,名称和销售数目的信息.水果先按产地分类,产地按字母顺序排列;同一产地的水果按照名称排序,名称按字母顺序排序.
两组测试数据之间有一个空行.最后一组测试数据之后没有空行.
 

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

Sample Output
guangdong |----pineapple(5) |----sugarcane(1)shandong |----apple(3)
一晚上的成果,自己琢磨出来了呢,嘻嘻,两个结构体,结构体排序,字符串排序。sort排序头文件是 #include <algorithm>
#include <iostream>#include <stdio.h>#include <stdlib.h>#include <algorithm>using namespace std;struct fruit{    int num;    string name;};struct location{    struct fruit fruit[105];    string province;    int q;}location[105];bool cmp(struct location a,struct location b){    if(a.province!=b.province)        return a.province<b.province;}bool cmm(struct fruit a,struct fruit b){    if(a.name!=b.name)        return a.name<b.name;}int main(void){   // freopen("C.txt","r",stdin);    int n;    string s_name;    string s_province;    int number;    cin>>n;    while(n--)    {            int m;           cin>>m;            int p=1;            for(int i=1;i<=m;i++)            {                 location[i].q=1;                cin>>s_name>>s_province>>number;                if(i==1)                {                    location[1].province=s_province;                    location[1].fruit[1].name=s_name;                    location[1].fruit[1].num=number;                }                else                {                   int ok2=0;                for(int j=1;j<=p;j++)                {                    if(location[j].province==s_province)                    {                        ok2=1;                        int ok1=0;                        for(int k=1;k<=location[j].q;k++)                        {                            if(location[j].fruit[k].name==s_name)                            {                                ok1=1;                                location[j].fruit[k].num+=number;                                break;                            }                        }                        if(ok1==0)                        {                            location[j].q++;                            location[j].fruit[location[j].q].num=number;                            location[j].fruit[location[j].q].name=s_name;                        }                        break;                    }                }                if(ok2==0)                {                    p++;                    location[p].province=s_province;                    location[p].fruit[1].num=number;                    location[p].fruit[1].name=s_name;                }                }            }            sort(location+1,location+p+1,cmp);        for(int i=1;i<=p;i++)            {                 cout<<location[i].province<<endl;                 sort(location[i].fruit+1,location[i].fruit+location[i].q+1,cmm);                for(int j=1;j<=location[i].q;j++)                {                    cout<<"   |----"<<location[i].fruit[j].name<<"("<<location[i].fruit[j].num<<")"<<endl;;                }            }            if(n)                cout<<endl;    }    return 0;}

0 0
原创粉丝点击