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 ...
随机推荐
- 【C++/实验三】类和对象
1.定义一个矩形类,有长,宽两个属性,有成员函数计算矩形的面积. 在该矩形类中,我做了5个主要的测试. 构造函数带默认值参数,利用默认值参数计算矩形面积:rectangle(double x=2.0, ...
- vue的单向数据流
父级向子组件传递的值, 子组件不能直接修改这个穿过来的值,否则会曝出警告,这就是单项数据流. 如果是引用值,传递的是引用值得地址,而不是值本身,也就是说,子组件里修改这个传过来的值,通常的做法是放到它 ...
- js运用2
1.变量提升 变量提升是浏览器的一个功能,在运行js代码之前,浏览器会给js一个全局作用域叫window,window分两个模块,一个叫内存模块,内存模块找到当前作用域下的所有的带var和functi ...
- C和C指针小记(十八)-使用结构和指针-双向链表
1.双链表 1.1 双向链表的声明 在一个双链表中,每个节点都包含两个指针--指向前一个节点的指针和指向后一个节点的指针. 声明 typedef struct NODE { struct NODE * ...
- orange---openresty.
需要有一个类似于API网关的中间件来做API的管理工作,也就是API的实现方面更多的考虑业务逻辑,安全.性能.监控可以交由网关来做(如下图所示,下图根据Kong官方文档修改) 通过MySQL存储来简单 ...
- 好用的 over the wall教程
还在为翻 xxx墙苦恼吗,一分钟就能搞定的翻xxx墙教程 1.下载chrome扩展插件 Proxy SwitchyOmega,加入到谷歌的高级扩展程序当中,这个就不详细讲解了. 请戳 https:// ...
- keras实现textcnn
https://github.com/MoyanZitto/keras-cn/blob/master/docs/legacy/blog/word_embedding.md 这个链接将带有embedin ...
- LaTeX大于小于号
发现大部分人只回答大于等于号.小于等于号的写法,而没有说大于.小于号的分别写法. 大于号:\textgreater 小于号: \textless 下面的后面要加空格,否则会识别错误 大于等于:\geq ...
- MongoDB常用操作--简介
mongodb在项目中使用越来越觉得日志方面的记录和查询是远远优于MySQL的,所以对其一些基本的操作进行了整理,以下就是整理的一些规则插入数据: insert插入多组数据:inserrAll修改数据 ...
- python数据结构-如何让字典有序
如何让字典有序 问题举例: 统计学生的成绩和名次,让其在字典中按排名顺序有序显示,具体格式如下 {'tom':(1, 99), 'lily':(2, 98), 'david':(3, 95)} 说明 ...