zoukankan      html  css  js  c++  java
  • HDU 1004 Let the Balloon Rise (map)

    题目链接

    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
    

    分析:

    将当前输入的字符串存放在一个数组中,数组定义为string型,可以用一个位置存储一个数组。

    一个int型的数组存放当前字符串与之相同的个数(不包括本身),找出最大值所对应的字符串即为所求。

    代码:

    #include<stdio.h>
    int main()
    {
        int n,m;
        while(~scanf("%d%d",&n,&m)&&n!=0&&m!=0)
        {
            int i=0,j=0,a[205]= {0},t=0;
            while(1)
            {
                j++;
                if(a[j%n]==0)
                    i++;
                if(i==m)
                {
                    i=0;
                    a[j%n]=1;
                    t++;
                    if(j%n==0)
                        printf("%d
    ",n);
                    else
                        printf("%d
    ",j%n);
                }
                if(t==n-1)
                    break;
            }
        }
        return 0;
    }
    #include <iostream>
    #include<stdio.h>
    #include<string>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    int main()
    {
       int n;
       while(cin>>n&&n)
       {
          int b[10002];
          string a[10002];//sting 的数组,用于存放当前输入的字符串,a[i]即表示为一个字符串
          memset (b,0,sizeof (b));
          for(int i=0;i<n;i++)
          {
              cin>>a[i];
              for(int j=0;j<i;j++)
              {
                  if(a[j]==a[i])//将当前输入的字符串与之前所有的进行比较,
                    b[j]++;//b中的下标代表的即为输入的字符串的序列,为当前字符串的个数
              }
          }
          int n1=0,max=b[0];
          for(int i=1;i<n;i++)
          {
              if(b[i]>max)
              {
                  max=b[i];
                  n1=i;
              }
          }
          cout<<a[n1]<<endl;
       }
        return 0;
    }
    

    可以简单的用一个map容器来解决

    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    #include<map>
    using namespace std;
    int main()
    {
        map<string,int>m;//一个map容器
        int n;
        string s,s1;
        while(scanf("%d",&n),n)
        {
            int max1=-1;
            while(n--)
            {
               cin>>s;
                m[s]++;//s为当前输入的一个字符串,m[s]为串的个数
                if(m[s]>max1)
                {
                    max1=m[s];
                    s1=s;
                }
            }
            cout<<s1<<endl;
        }
        return 0;
    }
  • 相关阅读:
    vue-cli3中热更新失效,修改完代码之后需要手动刷新页面才能看到改变,解决办法
    数组中的数据项包含逗号则需在首尾拼接中括号[]来区分每一项,最后数组转为字符串,以及数据恢复
    组件之间的拖拽
    工作心得
    Vue重点知识
    vue-router路由
    利用注解和反射,将Bean枚举字段的值填入相应的字段中,并转化为fastjson返回前台
    db2 获取自增主键的方法
    mybatis注解@selectKey对于db2数据库的使用
    @InsertProvider 根据bean属性,自动生成插入sql语句
  • 原文地址:https://www.cnblogs.com/cmmdc/p/6764377.html
Copyright © 2011-2022 走看看