欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

HDU 1004 Let the Balloon Rise (map)

程序员文章站 2024-02-15 18:13:04
...

题目链接

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",n);
                else
                    printf("%d\n",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;
}