zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    sql 笔记之一
    js的Location
    VS无法使用.XXXX附加到程序
    C#遍历对象的方法
    解决tomcat启动时中文乱码问题。
    动态sql foreach 循环报错问题
    java跳过https证书直接请求工具类
    安装svn报2503错误处理方法
    oracle常用的一些查询命令
    Oracle11g安装过程中忘记进行口令配置
  • 原文地址:https://www.cnblogs.com/puwen/p/9265510.html
Copyright © 2011-2022 走看看