01月05, 2019

hdu1004

题目描述:

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 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.

输出:

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.

样本输入:

5
green
red
blue
red
red
3
pink
orange
pink
0

样本输出:

red
pink

将每一个颜色看作二维数组,最多1000行,15列,使用c语言中的strcmp函数进行字符串比较大小,先把重复字符串的个数放入另一数组中,再找出另一数组的最大值即为原字符串数组出现次数最多的字符串的个数

代码:

#include <stdio.h>
#include <string.h>
#include <malloc.h>

int main()
{
    int n;
    char s[1000][15];
    int a[1000];
    while(scanf("%d",&n)!=EOF)
    {
        if(n)
        {
            memset(s,0,sizeof(s));
            memset(a,0,sizeof(a));
            for(int i=0;i<n;i++)
            {
                scanf("%s",s[i]);
                for(int j=0;j<i;j++)
                    if(strcmp(s[i],s[j])==0)
                        a[j]++;
            }
            int max=-1;
            int k=-1;
            for(int i=0;i<n;i++)
                if(max<a[i])
                {
                    max=a[i];
                    k=i;
                }
            printf("%s\n",s[k]);
        }
        else
            break;
    }
    return 0;
}

本文链接:https://lyuly.com/post/hdu1004.html

-- EOF --

Comments

评论加载中...

注:如果长时间无法加载,请针对 disq.us | disquscdn.com | disqus.com 启用代理。