HDU1004 Let the Balloon Rise

作者在 2013-05-24 11:36:14 发布以下内容

思路:

首先读取第一个表示颜色的字符串保存为结构体数组的第一个元素,个数为 1,结构体元素数个为1

然后每读取一个颜色字符串,就到结构体数组中寻找相同的字符串,如果找到,个数加1

如果没找到,在结构体中添加一个元素,个数为1,结构体元素个数加1


最后以结构体元素个数为标记,循环查找个数最大的一个元素,输出


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LEN 20

struct color{
	short num;
	char str[LEN];
};

int main(void)
{
	int groups;
	char tmp[LEN];
	int i, j, flag;
	int count;
	int max;
	struct color * pst;
	char * most;

	while(scanf("%d", &groups) == 1 && groups > 0 && groups <= 1000)
	{
		getchar();

		pst = (struct color *)malloc(groups * sizeof(struct color));
		memset(pst, 0, groups * sizeof(struct color));
		gets(pst[0].str);
		pst[0].num = 1;
		count = 1;			/*不同颜色个数*/
		
		for(i = 1; i < groups; i++)
		{
			gets(tmp);
			flag = 0;
			for(j = 0; j <= i; j++)
				if(strcmp(pst[j].str, tmp) == 0)
				{
					pst[j].num++;
					flag = 1;
				}
			
			if(!flag)
			{
				strcpy(pst[i].str,tmp);
				pst[i].num++;
				count++;
			}
		}
		
		max = pst[0].num;
		most = pst[0].str;
		for(i = 0; i < count; i++)
			if(pst[i].num > max)
			{
				max = pst[i].num;
				most = pst[i].str;
			}
			
		puts(most);
		free(pst);
	}
	return 0;
}

HDU | 阅读 1028 次
文章评论,共0条
游客请输入验证码
浏览39541次