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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,490评论 0 10
  • 洒字加一便成酒 绝妙联想成桥段 来日豪饮莫忘我 喜迎秋风来作伴 附注:网友"东门滴"联想丰富,在点评我的"秋风"一...
    沙雅达人阅读 155评论 0 0
  • 小学三年级有一次下雨我没带伞,放学很久了你去接我,同学跟我说你哥哥来了。那时我多不开心啊,你明明是我爸爸,哪有那么...
    亦清syp阅读 704评论 0 2
  • 家居南山,前溪后花,左青右翠 家居南山,鸟鸣恋山,风声喜林 家居南山,人稀曲幽,人欢禽善 家居南山,雎鸠抖翅,蒹葭...
    小屋的屋阅读 322评论 0 5
  • Taos国际系统排列学院排挤师五班一组王菲第一讲《在爱中升华》一至四章读书心得:一至四章主要讲述罪恶感、清白感、爱...
    王菲_0d7a阅读 253评论 0 0