HDU 1004 Let the Balloon Rise

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
 

Author
WU, Jiazhi
 

Source

ZJCPC2004


题目大意:

就是先输入需要输入的颜色个数,然后把每一个颜色的名字输入进去,最终输出相同颜色最多的那个颜色名称。

解题思路:

首先设置两个数组,一个是二维数组,一个是一维数组,二维数组是存放输入的颜色字符串,方便对比,一维数组是为了存放相同字符串个数,统计个数时是将当前数组位置存放的字符串与其后存放的字符串进行比较,获得与之相同的数量,并存进一维数组中作为记录,这其中数值最大的就是相同颜色最多的,并利用提取出其对应字符串进行输出。我认为输出相同颜色最多的名称这个算法很有趣,跟我之前想的有些不太一样,我的思路是找到一维数组中的最大值,直接将其下标数值提取并在二维数组对应的位置直接输出名称就好。

 #include <stdio.h>
 #include <string.h>
 int main()
{
	int n,max,i,j; 
    char a[1000][1000],b[1000];
    while(scanf("%d",&n)&&n)//while循环,输入n,因为题目有要求说当输入0时,即n=0时,整个程序结束,所以用了&&n来实现这个功能 
	{
		for(i=0;i<n;i++)
		{
			scanf("%s",&a[i]);
			b[i]=0;
		}
		for(i=0;i<n;i++)
		{
			for(j=i+1;j<n;j++)//j=i+1是自己和自己比较二造成相等是结果比正常结果大1
			{
				if(strcmp(a[i],a[j])==0)
				{
					b[i]++;
				}
			} 	
		}
		max=0;
		for(i=0;i<n;i++)//b[i]是与第I个颜色同色的个数 
		{
			if(max<b[i])//最多的选择,设计很巧妙,这个算法很有趣
			{
				max=i;//
			} 
		} 
		printf("%s\n",a[max]);//
    }
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值