现在的位置: 首页 > 综合 > 正文

Hdu 1029 Ignatius and the Princess IV (鸽巢原理)

2013年09月01日 ⁄ 综合 ⁄ 共 357字 ⁄ 字号 评论关闭

这道题非常水,主要目的是想把网上看到的一种解法收藏下……

思路:本题方法很多:map,哈希,排序(中位数)……网上看到一种利用鸽巢原理的,代码如下:

转自:http://blog.csdn.net/welcome_z/article/details/7855032

#include <cstdio>
#include <cstdlib>

int main ()  
{
	int ans,num,count,n;
	while (~scanf("%d", &n))
	{
		count = 0;
		for (int i=1; i<=n; i++)  
		{
			scanf("%d", &ans);
			if (count == 0)
			{
				num = ans;
				count ++;
			}
			else
				if (ans == num)
					count++;
				else count--;
		}
		printf("%d\n", num);
	}  
   return 0;
}

抱歉!评论已关闭.