zoukankan      html  css  js  c++  java
  • SDNU 1125.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

    Source

    思路:这道题,一开始我想了很多,比如用map把string和int存起来,然后用vector把map里的内容存起来再排序。后来事实证明是我想多了,完全可以用map的迭代器把map里的东西存入结构体,再排个序,输出就完事儿~
    #include <cstdio>
    #include <iostream>
    #include <cmath>
    #include <string>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <map>
    using namespace std;
    #define ll long long
    
    map<string, int>mp;
    
    struct node
    {
        string s;
        int num;
    } r[1000+8];
    
    int n;
    string si;
    
    bool cmp(node a, node b)
    {
        return a.num>b.num;
    }
    
    int main()
    {
        while(~scanf("%d", &n) && n != 0)
        {
            mp.clear();
            for(int i = 0; i<n; i++)
            {
                cin>>si;
                if(mp[si])mp[si]++;
                else mp[si] = 1;        }
            int id = 0;
            for(map<string, int>::iterator ii = mp.begin(); ii != mp.end(); ii++)
            {
                r[id].s = ii->first;
                r[id].num = ii->second;
                id++;
            }
            sort(r, r+id, cmp);
            cout<<r[0].s<<endl;
        }
        return 0;
    }
  • 相关阅读:
    HDFS文件读写操作(基础基础超基础)
    LeetCode & Q35-Search Insert Position-Easy
    LeetCode & Q27-Remove Element-Easy
    LeetCode & Q26-Remove Duplicates from Sorted Array-Easy
    LeetCode & Q1-Two Sum-Easy
    Full-Stack-Fundation-Udacity------Lesson 1 Working with CRUD
    利用封装、继承对Java代码进行优化
    排序算法——冒泡排序
    排序算法——希尔排序
    排序算法——直接插入排序
  • 原文地址:https://www.cnblogs.com/RootVount/p/10981994.html
Copyright © 2011-2022 走看看