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 ...
随机推荐
- Windows 平台下局域网劫持测试工具 – EvilFoca
简介 安全测试工具可能含有攻击性,请谨慎适用于安全教学及学习用途,禁止非法利用! EvilFoca是Windows环境下基于.NET FrameWork的一款轻量级的劫持测试工具.与BackTrack ...
- 关于微信里wx.getUserInfo获取用户信息都是拼音的转成中文方法
加一个参数:lang:"zh_CN" 就可以了 1. 加在js里面 wx.getUserInfo({ lang:"zh_CN", success: func ...
- 时时监控的rtsp流视频显示在前端与一些css;
不过试了下只兼容IE. <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- 前端 jquery获取当前页面的URL信息
以前在做网站的时候,经常会遇到当前页的分类高亮显示,以便让用户了解当前处于哪个页面.之前一直是在每个不同页面写方法.工程量大,也不便于修改.一直在想有什么简便的方法实现.后来在网上查到可以用获取当前U ...
- 电子产品使用感受之—我的iPad Pro坏了。。。
2018年2月,我下单了搭载A10X Fusion处理器.256GB存储的12.9寸iPad Pro,同时购买了AppleCare来保护我的iPad Pro.AppleCare价格虽然相对iPhone ...
- phpredis Redis集群 Redis Cluster
官方url: https://github.com/phpredis/phpredis/blob/develop/cluster.markdown#readme 2017年10月29日20:44:25 ...
- WinDbg 之 SOS扩展命令
SOS.dll (SOS debugging extension) The SOS Debugging Extension (SOS.dll) helps you debug managed prog ...
- 《Maven实战》文字版[PDF]
从亚马逊买的电子书,导出来的,需要的下吧. 下面是截图: 除了代码部分有一点点不清楚之外,其他还是蛮清楚的. 下载地址: http://download.csdn.net/download/apple ...
- [math] sagemath
官网首页:http://www.sagemath.org 首页里引出的两个教程 http://www.gregorybard.com/Sage.html http://sagebook.gforge. ...
- [redis] 与redis cluster有关的学习笔记
主要是以下三个官方文档,只略读了前两个,第三个还没有读. <redis cluster tutorial> <redis sentinel> <redis cluster ...