C语言解题——Let the Balloon Rise

C语言解题

每天更新一道OJ题目,今日介绍for 和 while 循环的使用方法和示例。


前言

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.

输入包含多个测试用例。每个测试用例都以数字 N (0 < N <= 1000) 开头 - 分布的气球总数。接下来的 N 行各包含一种颜色。气球的颜色是最多包含 15 个小写字母的字符串。N = 0 的测试用例终止输入,并且不处理此测试用例。

二、输出

       

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.For each case, print the balloon color of the hottest issue on a single line. You can ensure 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<stdio.h>
#include<string.h>
int main()
{
    int test;     //测试用例数
    
    while(scanf("%d",&test),test)
    {  
        getchar();          
        char color[1010][20];     //定义一个二维字符数组
        for(int i=0; i<test; i++)
        {
            scanf("%s",&color[i]);    //输入颜色
        }
        int k;
        int max=-1000,sum=0;
        for(int i=0; i<test; i++)   //类似冒泡排序,这里进行比较
        {
            sum=0;
            for(int j=0; j<test; j++)
            {
                if(strcmp(color[i],color[j])==0)   //如果有重复的字符串,则sum计数加一,
                {
                    sum++;
                }
            }
            if(sum>max)     //寻找出现次数最多的颜色
            {
                max=sum;
                k=i;
            }
        }
       printf("%s\n",color[k]);
    }
    return 0;
}

以上就是今天要讲的内容。

posted @ 2022-07-15 08:45  Xa_L  阅读(12)  评论(0编辑  收藏  举报  来源