题目:http://acm.hdu.edu.cn/showproblem.php?pid=1263

文章末有相应的一些测试数据供参考。

传统的数组解题方式

思路一:

三种属性的数据放在一个结构体里面,然后按照题目要求排序。

输出处理的时候,遍历一遍,边统计边输出,因为排序并没有进行统计。

思路二:

同样是一个结构体,然后排序。

再做一个预处理——遍历一遍,如果产地和品种都一样,将数目加在后者上面,前者数目清零

将处理好的数组直接遍历输出(一个for循环里面两个 if 分别控制输出两类数据):

如果当前产地和前一个的产地不一样,输出产地

如果当前元素中水果数量不为零,输出水果信息

代码如下

两种思路前面的代码都一样:

 #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct INFO
{
string area, fruit;
int num;
}info[]; inline bool my_cmp(const INFO& lhs, const INFO& rhs)
{
if (lhs.area == rhs.area)
return lhs.fruit < rhs.fruit;
return lhs.area < rhs.area;
}

思路一main函数代码:

int main()
{
int group;
cin >> group;
while (group--)
{
int n;
cin >> n;
for (int i = ; i < n; ++i)
cin >> info[i].fruit >> info[i].area >> info[i].num;
sort(info, info + n, my_cmp);
for (int i = ; i < n; ++i)
{
cout << info[i].area << endl;
bool key = false;
while (i + < n&&info[i + ].area == info[i].area) //如果产地一样
{
if (key) {
i++; //i 加了 1
if (info[i + ].area != info[i].area) { //下一个的产地可能和当前的产地不一样
cout << " |----" << info[i].fruit << "(" << info[i].num << ")" << endl;
break;
}
}
key = true;
int number = info[i].num;
while (i + < n&&info[i + ].fruit == info[i].fruit) //如果水果一样
{
i++;
number += info[i].num;
}
cout << " |----" << info[i].fruit << "(" << number << ")" << endl;
}
if (!key)
cout << " |----" << info[i].fruit << "(" << info[i].num << ")" << endl;
}
if (group)cout << endl;
}
}

思路二main函数代码:

 int main() {
int Group, m;
cin >> Group;
while (Group--) {
int j = ;
cin >> m;
for (int i = ; i < m; i++)
cin >> info[i].fruit >> info[i].area >> info[i].num;
sort(info, info + m, my_cmp);
//再次预处理
for (int i = ; i<m; i++){
if (info[i].area==info[i-].area&&info[i].fruit==info[i-].fruit){
info[i].num += info[i - ].num;
info[i - ].num = ;
}
}
cout << info[j].area << endl;
for (j = ; j<m; j++){
if (j != && info[j].area != info[j - ].area) //只要产地不一样就输出产地信息
cout << info[j].area << endl;
if (info[j].num != ) //只要数量不为0就输出水果信息
cout << " |----" << info[j].fruit << "(" << info[j].num << ")" << endl;
}
if (Group) cout << endl; //只要不是最后一组数据就打一个空行
}
return ;
}

STL中map嵌套解题方式

其思路同二,只不过map不需要分类

(代码转自:http://blog.csdn.net/u012861385/article/details/19038865)

 #include<iostream>
#include<map>
#include<string>
using namespace std;
int main()
{
int N, M;
int i,j,k;
string name, place;
int value;
scanf("%d", &N);
while(N--)
{
map<string,map<string, int> > str; //先按地点,第一个string排序插入,之后按照第二个string名称排序插入//
map<string,map<string, int> >::iterator it;
map<string,int>::iterator iw;
scanf("%d", &M);
for(i = ; i <= M; i++)
{
cin >> name >> place >> value;
str[place][name] += value;
}
for(it = str.begin(); it != str.end(); it++)
{
cout << it->first <<endl;
for(iw = it->second.begin(); iw != it->second.end(); iw++)
{
cout<<" |----"<<iw->first<<"("<<iw->second<<")"<<endl;
}
}
if(N != )
cout << endl;
}
return ;
}

参考数据(本人):http://acm.hdu.edu.cn/discuss/problem/post/reply.php?postid=34666&messageid=1&deep=0

对应的output如下

   |----a()
|----b()
|----c()
|----d() |----a()
|----b()
|----c()
|----d() |----a() |----a()
|----b() |----b()

谢谢您的阅读,祝您生活愉快~

ACM 水果 hdu 1263 一题多解的更多相关文章

  1. SQLServer 常见SQL笔试题之语句操作题详解

    SqlServer 常见SQL笔试题之语句操作题详解 by:授客 QQ:1033553122 测试数据库 CREATE DATABASE handWriting ON PRIMARY ( name = ...

  2. 牛客网 Java 工程师能力评估 20 题 - 详解

    牛客网 Java 工程师能力评估 20 题 - 详解 不知在看博客的你是否知道 牛客网,不知道就太落后了,分享给你 : 牛客网 此 20 题,绝对不只是 20 题! 免责声明:本博客为学习笔记,如有侵 ...

  3. 关于SQL的几道小题详解

    关于SQL的几道小题详解 当我们拿到题目的时候,并不是急于作答,那样会得不偿失的,而是分析思路,采用什么方法,达到什么目的,还要思考有没有简单的方法或者通用的方法等等,这样才会达到以一当十的效果,这样 ...

  4. HDU-1042-N!(Java大法好 &amp;&amp; HDU大数水题)

    N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Subm ...

  5. 一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]

    ASP.NET Core应用本质上就是一个由中间件构成的管道,承载系统将应用承载于一个托管进程中运行起来,其核心任务就是将这个管道构建起来.在ASP.NET Core的发展历史上先后出现了三种应用承载 ...

  6. 一题多解,ASP.NET Core应用启动初始化的N种方案[下篇]

    [接上篇]"天下大势,分久必合,合久必分",ASP.NET应用通过GenericWebHostService这个承载服务被整合到基于IHostBuilder/IHost的服务承载系 ...

  7. HDU 5122 K.Bro Sorting(模拟——思维题详解)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an A ...

  8. hdu 1263 水果 【二维map】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1263 题目大意: Problem Description 夏天来了~~好开心啊,呵呵,好多好多水果~~ ...

  9. 题解报告:hdu 1263 水果

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1263 Problem Description 夏天来了~~好开心啊,呵呵,好多好多水果~~ Joe经营 ...

随机推荐

  1. 【数据库】软件安全测试之SQL注入

    这些年我们发现越来越多的公司开始注重安全测试了,为什么?因为安全测试可以在某种程度上可以排查掉你项目的一些安全漏洞,这样你的系统上线后才会相对安全,才有可能尽量避免来自外部的攻击.每一年互联网都会发生 ...

  2. TCP确认延时和Nagle算法

    TCP确认延时和Nagle算法 nagle 算法是   发送端 收到前一个报文的确认然后再发送下一个tcp数据.这样可以避免大量的小数据. TCP_NODELAY选项控制. Delay ACK是   ...

  3. Java设计模式——工厂模式

    一.工厂模式分类 工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的. 工厂模式在<Java与模式>中分为三类: (1)简单工厂模式(Simp ...

  4. 20165230 《Java程序设计》实验二(Java面向对象程序设计)实验报告

    20165230 <Java程序设计>实验二(Java面向对象程序设计)实验报告 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:田坤烨 学号:20165230 成绩: ...

  5. 生成Word/ATU报表提示 font family not found

    1.先从你本机 C:\Windows\Fonts 拷贝或者网络上下载你想要安装的字体文件(*.ttf文件)到 /usr/share/fonts/chinese/TrueType 目录下(如果系统中没有 ...

  6. 聊天室(下篇)GatewayWorker 与 Laravel 的整合

    思路 上一篇大概梳理了一下 GatewayWorker 的基础知识.这篇就来准备整合 GatewayWorker 到 Laravel. GatewayWorker 是基于 Socket 监听的服务器框 ...

  7. Resouce, platform_device 和 platform_driver 的关系【转】

    转自:http://blog.csdn.net/uruita/article/details/7278313 從2.6版本開始引入了platform這個概念,在開發底層驅動程序時,首先要確認的就是設備 ...

  8. python socket编程和黏包问题

    一.基于TCP的socket tcp是基于链接的,必须先启动服务端,然后再启动客户端去链接服务端,有顺序,不重复,可靠.不会被加上数据边界. server端 import socket sk = so ...

  9. 33 Introducing the Go Race Detector

    Introducing the Go Race Detector 26 June 2013 Introduction Race conditions are among the most insidi ...

  10. php修改文件上传大小限制

    上传一个20M文件的时候php报如下错误,是php上传文件大小限制引起 POST Content-Length of 19248654 bytes exceeds the limit of 83886 ...