Problem Description
夏天来了好开心啊,呵呵,好多好多水果
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)
结构体
原博客https://blog.youkuaiyun.com/libin56842/article/details/8998471
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct Node
{
char name[100];
char space[100];
int num;
} f[110];
int cmp(Node x,Node y)
{
if(strcmp(x.space,y.space))
return strcmp(x.space,y.space)<0;
return strcmp(x.name,y.name)<0;
}
int main()
{
int t,n,i;
scanf("%d",&t);
while(t--)
{
scanf("%d%*c",&n);
for(i = 0; i<n; i++)
{
scanf("%s%s%d",f[i].name,f[i].space,&f[i].num);
}
sort(f,f+n,cmp);
char di[100],min[100];
int cnt = 0,flag = 1;
strcpy(di,f[0].space);
strcpy(min,f[0].name);
for(i = 0; i<n; i++)
{
if(strcmp(di,f[i].space))
{
strcpy(di,f[i].space);
strcpy(min,f[i].name);
flag = 1;
cnt = 0;
}
if(!strcmp(di,f[i].space))
{
if(flag)
{
printf("%s\n",di);
flag = 0;
}
if(!strcmp(min,f[i].name))
{
while(!strcmp(min,f[i].name) && !strcmp(di,f[i].space))//产地与水果名都必须相同
{
cnt+=f[i].num;
i++;
}
printf(" |----%s(%d)\n",min,cnt);
strcpy(min,f[i].name);
i--;
cnt = 0;
}
}
}
if(t)
printf("\n");
}
return 0;
}
map
原博客https://blog.youkuaiyun.com/nkkkkk/article/details/86570959
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
for(int cas=0;cas<t;cas++)
{
map<string,map<string,int> >p;
map<string,map<string,int> >::iterator it;
map<string,int>::iterator kk;
int n;
scanf("%d",&n);
string name,place;
int num;
for(int i=0;i<n;i++)
{
cin>>name>>place>>num;
///string不能用%s
p[place][name]+=num;
}
for(it=p.begin();it!=p.end();it++)
{
cout<<it->first<<endl;
for(kk=it->second.begin();kk!=it->second.end();kk++)
{
cout<<" |----"<< kk->first <<"("<< kk->second <<")"<<endl;
}
}
if(cas!=t-1)
printf("\n");
}
return 0;
}
*搬运大佬博客,侵必删
本文介绍了一种算法,用于生成水果销售明细表,通过结构体和map数据结构,实现了按产地和水果名称排序的销售数据展示。示例代码使用C++实现,包括输入输出示例。
5177

被折叠的 条评论
为什么被折叠?



