原题链接: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 [链表解法]的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. hdu 1004 Let the Balloon Rise

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  5. 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 ...

  6. 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 ...

  7. 杭电1004 Let the Balloon Rise

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  8. HDOJ 1004 Let the Balloon Rise

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

  9. HDU 1004 - Let the Balloon Rise(map 用法样例)

    Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...

随机推荐

  1. mac sed 使用踩坑实录

    [转自别处] 比如我sed想做文件原地的替换,但是怎么写都出错,错误提示还莫名其妙,后来多方搜索才知道Mac上的sed如果参数有-i就必须加上备份指令,即-i后添加任意字符,那些字符就作为备份文件的后 ...

  2. ERP项目实施记录09

    今天报价软件测试版本出来了,可看上去不怎么像是一款报价的软件,整个界面上都没有"报价"相关的字眼: 软件标题就不说了,反正影响不大,就当没看见,可左边这一大片菜单里也找不到和报价有 ...

  3. 一个完整的hadoop程序开发过程

    目的 说明hadoop程序开发过程 前提条件 ubuntu或同类OS java1.6.0_45 eclipse-indigo hadoop-0.20.2 hadoop-0.20.2-eclipse-p ...

  4. Linux:使用rpcgen实现64位程序调用32位库函数

    摘要:本文介绍使用rpcgent实现64位程序调用32位库函数的方法,并给出样例代码. 我的问题 我的程序运行在64位Linux系统上,需要使用一个从外部获得的共享库中的函数,这个共享库是32位的,无 ...

  5. The way to unwind the stack on Linux EABI

    I. probe the stack frame structure The original idea is to unwind the function call stack according ...

  6. 20144306《网络对抗》MAL_恶意代码分析

    一.基础问题 1.如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪些,用什么方法来监控? 使用Windows自带的schtasks指 ...

  7. 数据类型&字符串得索引及切片

    一:数据类型 1):int     1,2,3用于计算 2):bool    ture  false  用于判断,也可做为if的条件 3):str     用引号引起来的都是str 存储少量数据,进行 ...

  8. jQuery:实现图片按需加载的方法,当要显示内容的高度超过了页面的高度,按需加载,根据滚动条的位置来判断页面显示的内容

    实现图片按需加载的方法,当要显示内容的高度超过了页面的高度,按需加载,根据滚动条的位置来判断页面显示的内容 这个类似于京东或淘宝页面,根绝页面的滚动,显示下面的内容 如下图所示,一开始并不是所有的图片 ...

  9. Python tesserocr模块使用示例

    操作系统:Win10 1709  X64 python版本:3.6.5 依赖模块:PIL.tesserocr. 需要说明的是,在windows系统上PowerShell通过PIP3 install t ...

  10. 把ResNet-L152模型的ckpt文件转化为pb文件

    import tensorflow as tf from tensorflow.python.tools import freeze_graph #os.environ['CUDA_VISIBLE_D ...