zoukankan      html  css  js  c++  java
  • HDU-1004 Let the Balloon Rise

    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 <iostream>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    struct color
    {
        char str[20];
        int num;
    }s[101];
    int k = 0;
    
    int main(void)
    {
        int t;
        void func(color *, char *);
        s[0] = {'', 0};
        bool sort_func(color a, color b);
        
        while(cin >> t && t)
        {
            k = 0;
            char inp[20];
            int i;
            
            for(i = 0; i < t; i++)
            {
                cin >> inp;
                func(s, inp);
            }
            sort(s, s+k, sort_func);
            cout << s[0].str << endl;
        }
            
        
        
        return 0;
    }
    
    
    bool sort_func(color a, color b)
    {
        return a.num > b.num;
    }
    
    
    void func(color *a, char *b)
    {
        for(int j = 0; j <= k; j++)
        {
            if(!strcmp((*(a+j)).str, b))
            {
                a[j].num++;
                return;
            }
        }
        strcpy(a[k].str, b);
        a[k++].num = 1;
        
        return;
    }
    
  • 相关阅读:
    luogu 1169 棋盘制作(单调栈/悬线)
    poj 2769 感觉♂良好 (单调栈)
    hdu 5033 buiding(单调栈)
    hdu1506 直方图中最大的矩形 单调栈入门
    有线电视网(树形dp)
    洛谷P1220 关路灯(区间dp)
    【题解】NOI2009二叉查找树 + NOIP2003加分二叉树
    【题解】AHOI2009中国象棋
    【算法】Matrix
    【题解】WC2008游览计划
  • 原文地址:https://www.cnblogs.com/limyel/p/6671336.html
Copyright © 2011-2022 走看看