HDU 1263(水果统计 **)

题意是对水果的产地和种类进行统计再按格式输出。

代码如下:

 #include <bits/stdc++.h>
using namespace std;
struct node
{
char name[],place[];
int num;
}f[];
bool cmp(node x,node y)
{
if(strcmp(x.place,y.place)<) return true;
if(strcmp(x.place,y.place)==&&strcmp(x.name,y.name)<) return true;
return false;
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
memset(&f,,sizeof(&f));
scanf("%d",&n);
for(int i = ; i < n; ++i)
scanf("%s %s %d",f[i].name,f[i].place,&f[i].num);
sort(f,f+n,cmp);
for(int i = ; i < n; ++i)
{
if(strcmp(f[i].place,f[i+].place)==)
{
if(strcmp(f[i].name,f[i+].name)==)
{
f[i+].num += f[i].num;
f[i].num = ;//防止同种水果输出多次
}
}
else
{
printf("%s\n",f[i].place);
for(int j = ; j < n; ++j)
if(strcmp(f[i].place,f[j].place)==&&f[j].num)
printf(" |----%s(%d)\n",f[j].name,f[j].num);
}
}
if(t!=) printf("\n");
}
return ;
}
上一篇:题解报告:hdu 1263 水果


下一篇:Linux菜鸟——常见命令一 查看及创建目录文件等命令