hduoj#1004 -Let the Balloon Rise [链表解法]
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004
Problem Description
Contest time again! How excited it is to see balloons floating around. But to tell you a secret, the judges' favorite time is guessing the most popular problem. When the contest is over, they will count the balloons of each color and find the result.
This year, they decide to leave this lovely job to you.
Input
Input contains multiple test cases. Each test case starts with a number N (0 < N <= 1000) -- the total number of balloons distributed. The next N lines contain one color each. The color of a balloon is a string of up to 15 lower-case letters.
A test case with N = 0 terminates the input and this test case is not to be processed.
Output
For each case, print the color of balloon for the most popular problem on a single line. It is guaranteed that there is a unique solution for each test case.
Sample Input
5
green
red
blue
red
red
3
pink
orange
pink
0
Sample Output
red
pink
题目分析:判断出输入的彩色气球出现次数最多的颜色
解题思路:采用链表的数据结构
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* 气球的数据结构
* 存储中会按照color的值的比较从小到大存储
*/
struct balloon
{
char color[16]; // 气球的颜色
int sum; // 该颜色气球出现的总次数
struct balloon *next;
};
int main()
{
struct balloon *head,*p,*q;
int n,maxSum;
char color[16];
while (~scanf("%d",&n) && n)
{
maxSum = -1;
head = (struct balloon *) malloc(sizeof(struct balloon));
head->sum = 0;
head->next = NULL;
getchar();
while (n--)
{
gets(color);
// 如果head存储的就是color
if (!strcmp(head->color,color))
{
if (!head->sum)
strcpy(head->color,color);
head->sum++; // head的sum自增1
}
// 如果head中存储的color大于输入的color
else if (strcmp(head->color,color) > 0)
{
// 在head前新增结点保存color
p = head;
head = (struct balloon *) malloc(sizeof(struct balloon));
strcpy(head->color,color);
head->sum = 1;
head->next = p;
}
// 其他情况:head中存储的color小于输入的color
else
{
p = head;
// 遍历链表,直到p的下一个结点为空或p结点的color正好小于输入的color
while (p->next && strcmp(p->next->color,color) < 0)
p = p->next;
// 如果下一个结点为空,即最后一个结点中存储的color仍小于输入值
if (!p->next)
{
// 在链表尾部新增结点
q = (struct balloon *) malloc(sizeof(struct balloon));
strcpy(q->color,color);
q->sum = 1;
q->next = NULL;
p->next = q;
}
// 如果p结点的color正好小于输入值且p结点的下一个结点的color大于输入值
else if (strcmp(p->next->color,color) > 0)
{
// 在p结点与p结点的下一个结点之间插入新增的结点q
q = (struct balloon *) malloc(sizeof(struct balloon));
strcpy(q->color,color);
q->sum = 1;
q->next = p->next;
p->next = q;
}
// 其他情况:p结点的下一个结点的color大于输入值
else
// p结点的下一个结点的sum自增1
p->next->sum++;
}
}
p = head;
// 遍历查询出出现次数最多的气球的颜色
while (p)
{
if (p->sum > maxSum)
{
maxSum = p->sum;
strcpy(color,p->color);
}
p = p->next;
}
puts(color);
// 释放空间
while (p)
{
q = p->next;
free(p);
p = q;
}
}
return 0;
}
hduoj#1004 -Let the Balloon Rise [链表解法]的更多相关文章
- hdu 1004 Let the Balloon Rise(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004 Let the Balloon Rise Time Limit: 2000/1000 MS (J ...
- HDU 1004 Let the Balloon Rise(map的使用)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1004 Let the Balloon Rise Time Limit: 2000/1000 MS (J ...
- HDU 1004 Let the Balloon Rise【STL<map>】
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- hdu 1004 Let the Balloon Rise
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- HDU 1004 Let the Balloon Rise map
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- hdu 1004 Let the Balloon Rise strcmp、map、trie树
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- 杭电1004 Let the Balloon Rise
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
- HDOJ 1004 Let the Balloon Rise
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- HDU 1004 - Let the Balloon Rise(map 用法样例)
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
随机推荐
- Christmas Spruce
Consider a rooted tree. A rooted tree has one special vertex called the root. All edges are directed ...
- vue中嵌套页面 iframe 标签
vue中嵌套iframe,将要嵌套的文件放在static下面: <iframe src="../../../static/bear.html" width="300 ...
- @staticmethod和classmethod
之前一直搞不清楚这两个类方法有什么区别,今天着重学习了一下 @staticmethod是静态方法,不需要表示自身对象的self和自身类的cls参数,就跟使用函数一样. class C(object): ...
- day20:序列化模块,模块的导入
1,什么是序列化--将原本的字典,列表等内容转换成一个字符串的过程就叫做序列化,字符串是有顺序的,序列化转向一个字符串的过程,我们平时说的序列,指的就是字符串. 2,为何要序列化?本来字符串是可以强转 ...
- qt ShaderEffect上的ShaderToy
https://zhuanlan.zhihu.com/p/38942460 发现这个挺好玩,有空学习一下
- 树莓派3 之 pi3Robot 控制系统配置
需求 个人正在用Python写一个控制系统,技术选型是python3 + Flask + Mysql + Bootstrap.需要将这套系统直接部署到树莓派中. 代码地址:https://github ...
- JS — 获取4个不重复的随机验证码
var strCode='zxcvbnmasdfghjklopiuytrewqAWEDRFTGYHUJIK'; var str=''; for(var i=0;i<4;i++){ var ran ...
- Docker入门4------Dockerfile
转自:https://www.cnblogs.com/jsonhc/p/7766841.html https://www.cnblogs.com/jsonhc/p/7767669.html Docke ...
- Centos下搭建邮件服务器
一.协议 SMTP:用于发送邮件 POP3:用于接收邮件,接收后会将服务器上邮件删除 IMAP:用于接收邮件,接收后不会删除服务器邮件 二.几个重要的角色 MUA:可以理解为收取邮件的工具,比如thu ...
- mybatis 转义
当我们需要通过xml格式处理sql语句时,经常会用到< ,<=,>,>=等符号,但是很容易引起xml格式的错误,这样会导致后台将xml字符串转换为xml文档时报错,从而导致程序 ...