题解报告:hdu 1263 水果
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1263
#include<bits/stdc++.h>
using namespace std;
struct node
{
char fruit[];//水果名称
char area[];//产地名称
int num;//交易次数
}c[];
bool cmp(node x,node y)//排序规则
{
if(strcmp(x.area,y.area)<)return true;//产地先从小到大排序
if(strcmp(x.area,y.area)== && strcmp(x.fruit,y.fruit)<)return true;//如果产地相同,则按照字典将水果排序
return false;//否则返回假
}
int main()
{
int N,M;
cin>>N;
while(N--){ //测试数据组数
cin>>M;
getchar(); //吃掉回车符
for(int i=;i<M;i++)//M次交易
cin>>c[i].fruit>>c[i].area>>c[i].num;
sort(c,c+M,cmp);//按规则排序
for(int i=;i<M;i++){
if(strcmp(c[i].area,c[i+].area)==){//如果当前产地与下一个产地相同
if(strcmp(c[i].fruit,c[i+].fruit)==){//且水果名称相同
c[i+].num+=c[i].num;//则将水果数目进行相加
c[i].num=;//将原来的水果数目清空
}
}
else{//如果不同
cout<<c[i].area<<endl;//不同就先输出产地
for(int j=;j<M;j++){//依次再进行比较
if(strcmp(c[i].area,c[j].area)== && c[j].num)//找到一个num是不为0的,即该产地某种水果的总数
cout<<" |----"<<c[j].fruit<<'('<<c[j].num<<')'<<endl;//输出最后的水果数
}
}
}
if(N!=)cout<<endl;//案例之间输出空行
}
return ;
}
下面是string版本:
#include<bits/stdc++.h>
using namespace std;
struct node
{
string fruit;//水果名称
string area;//产地名称
int num;//交易次数
}c[];
bool cmp(node x,node y)//排序规则
{
if(x.area!=y.area)return x.area<y.area;//产地先从小到大排序
return x.fruit<y.fruit;//如果产地相同,水果按字典排序
}
int main()
{
int N,M;
cin>>N;
while(N--){ //测试数据组数
cin>>M;
getchar(); //吃掉回车符
for(int i=;i<M;i++)//M次交易
cin>>c[i].fruit>>c[i].area>>c[i].num;
sort(c,c+M,cmp);//按规则排序
for(int i=;i<M;i++){
if(c[i].area==c[i+].area){//如果当前产地与下一个产地相同
if(c[i].fruit==c[i+].fruit){//且水果名称相同
c[i+].num+=c[i].num;//则将水果数目进行相加
c[i].num=;//将原来相同水果的数目清空
}
}
else{//如果不同
cout<<c[i].area<<endl;//不同就先输出产地
for(int j=;j<M;j++){//依次再进行比较
if((c[i].area==c[j].area) && c[j].num)//找到此时水果的最后一个计数num是不为0的,即为该产地某种水果的总数
cout<<" |----"<<c[j].fruit<<'('<<c[j].num<<')'<<endl;//输出最后的水果数
}
}
}
if(N!=)cout<<endl;//案例之间输出空行
}
return ;
}
题解报告:hdu 1263 水果的更多相关文章
- hdu 1263 水果 【二维map】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1263 题目大意: Problem Description 夏天来了~~好开心啊,呵呵,好多好多水果~~ ...
- HDU 1263 水果 结构体排序
解题报告:一个结构体排序的题,用了一个运算符重载,要注意的是不同的地方可能会产相同的水果,一开始没注意. #include<cstdio> #include<cstring> ...
- hdu 1263 水果
Problem Description 夏天来了~~好开心啊,呵呵,好多好多水果~~ Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表,这样J ...
- HDU 1263(水果统计 **)
题意是对水果的产地和种类进行统计再按格式输出. 代码如下: #include <bits/stdc++.h> using namespace std; struct node { ],pl ...
- hdu 1263 水果 结构的排序+sort自定义排序
水果 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...
- hdu 1263 水果 (嵌套 map)
水果Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissio ...
- HDU 1263 水果 (STL map)
水果 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submissi ...
- STL || HDU 1263 水果
map可以映射map…… 然后在map里面会自己排序 惊了 注意输出格式 回车的输出 #include <iostream> #include <cstdio> #includ ...
- 题解报告:hdu 1398 Square Coins(母函数或dp)
Problem Description People in Silverland use square coins. Not only they have square shapes but also ...
随机推荐
- c# 自定义Base16编码解码
一.自定义Base16编码原理 Base16编码跟Base64编码原理上有点不同,当然前面转换是一样的,都是是将输入的字符串根据默认编码转换成一 ...
- MySQL存储结构的使用
前言 今天公司老大让我做一个MySQL的调研工作,是关于MySQL的存储结构的使用.这里我会通过3个样例来介绍一下MySQL中存储结构的使用过程,以及一些须要注意的点. 笔者环境 系统:Windows ...
- Hibernate4之session核心方法
在学习session的核心方法之前,我们先了解下hibernate中几种对象的状态: 暂时状态:这样的状态就好像咱们公司请的暂时员工一样,他在公司里没有相关的资料和id. 特点:在使用代理主键的情况下 ...
- int&boolean——Java和C的一点小差别
Java和C的差别非常多.只是预计这一点非常多人都不知道. 今天面试时碰到这么道C语言题 求执行结果 int x = -1; while(!x!=0){ cout<<x<<en ...
- HTML的高富帅
1,前端的内容(组成部分有以下三部分) HTML CSS JS 裸体的人 穿上好看的衣服 ...
- Hackrank Equal DP
Christy is interning at HackerRank. One day she has to distribute some chocolates to her colleagues. ...
- select case when if
select case when if 的一些用法 - 马丁传奇 - 博客园 https://www.cnblogs.com/martinzhang/p/3220595.html Write a SQ ...
- id 查询
Ids Query | Elasticsearch Reference [6.2] | Elastic http://www.elastic.co/guide/en/elasticsearch/ref ...
- 运行tomcat6w.exe ,提示 指定的服务未安装 unable to open the service 'tomcat6'
错误:运行tomcat6w.exe ,提示 指定的服务未安装 unable to open the service 'tomcat6'(我用的是官网下载的解压版) 解决方法: 打开命令行提示符窗口=& ...
- leetcode 664. Strange Printer
There is a strange printer with the following two special requirements: The printer can only print a ...