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 ...
随机推荐
- 用 ArrayList 集合调用商品类
public class Commodity{ //定义商品类 String name; //定义商品名字 double size; //定义商品尺寸 double price; //定义商品 ...
- python的基础1
1.换行需要多个引号,'''''' """"""Eg:print('''文能提笔安天下,# 武能上马定乾坤.# 心存谋略和人生,# 古今银熊 ...
- 扫二维码登录实现原理,php版
基础的逻辑图就是这样,但是实际情况还是有几种可能 比如QQ登录,微信登录,微博登录,基本设计都差不多,根据实际情况会有一些差异 问题是,如果设计合理的接口在保证数据的安全性和快速性 设计到的技术不复杂 ...
- [httpd] httpd server 在低负载的情况下对SYN无响应
如题: 两台client通过load balance访问httpd server.两个client交互访问.load balance处于fullnat模式. server在低负载情况下,常常对某一个c ...
- 分析Hello2代码
代码如下String username = request.getParameter("username"); if (username != null && us ...
- linux affinity
现在的CPU几乎都是多核,所以,分配给予进程相同数量的线程是合理的需求 但是,这些线程不一定就均匀跑在这些内核上 所以,我们要指派,“一个线程就运行在一个固定的CPU内核上” //test.c #de ...
- nuxtJs中直接使用自带的@nuxtjs/axios
最初我以为在nuxtjs中是需要重新npm install axios,但是其实nuxtjs自己集成了这个数据渲染方法 你只需在nuxt.config.js中配置一下就可以了 modules: [ / ...
- j2ee高级开发技术课程第六周
一.jsf(java server faces)的运行原理(工作方式) 1.jsf应用是事件驱动的,当一个事件发生时(比如用户单击一个按钮),事件通知通过HTTP发往服务器,服务器端使用叫做Faces ...
- DelayQueue源码解析
DelayQueue是一个支持延时获取元素的无界阻塞队列.里面的元素全部都是“可延期”的元素,列头的元素是最先“到期”的元素,如果队列里面没有元素到期,是不能从列头获取元素的,哪怕有元素也不行.也就是 ...
- week_one-python格式化输出
1.多行格式化输出 (1) # Author:larlly name = input("input your name :") age = input("input yo ...