ACM1004: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
--------------------------------------------------------------------------------------------------
/*
简单题,统计气球数最多的颜色
*/
#include <stdio.h>
#define SIZE_ROW 1001
#define SIZE_COL 16

int main()
{
	int N;
	int i;

	char colors[SIZE_ROW][SIZE_COL];
	char color[SIZE_COL];
	//存储气球颜色数
	int colorNum;
	//统计不同颜色气球个数
	int balloon[SIZE_ROW];
	//用于返回重复颜色气球的下标
	int result;
	//颜色对多的气球个数
	int max;
	//颜色最多的气球个数的下标
	int maxIndex;
	//freopen("F:\\input.txt", "r", stdin);
	while ((scanf("%d", &N) == 1) && N != 0)
	{
		colorNum = 0;
		memset(balloon, 0, sizeof(balloon));
		memset(colors,0,sizeof(colors));
		for (i = 0; i < N; i++)
		{
			scanf("%s", color);
			//搜索颜色数组,如果没有当前输入的颜色,则将该颜色插入颜色数组。否则,对应的气球数+1
			result = searchColor(color, colors, colorNum, balloon);
			if (result == -1)
			{
				strcpy(colors[colorNum], color);
				balloon[colorNum]++;
				colorNum++;
			}
			else
				balloon[result]++;
		}
		max = balloon[0];
		maxIndex = 0;
		//寻找气球数最多的颜色
		for (i = 1; i < colorNum; i++)
		{
			if (max < balloon[i])
			{
				max = balloon[i];
				maxIndex = i;
			}
		}
		printf("%s\n", colors[maxIndex]);
	}
	//freopen("con", "r", stdin);
	//system("pause");
	return 0;
}

int searchColor(char color[], char colors[][SIZE_COL], int colorNum, int balloon[])
{
	int i;
	for (i = 0; i < colorNum; i++)
	{
		if (strcmp(color, colors[i]) == 0)
			return i;
	}
	return -1;
}

  

posted @ 2019-03-11 19:25  烈焰蔷薇  阅读(157)  评论(0编辑  收藏  举报