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

HDOJ 1029:Ignatius and the Princess IV 解题报告

2018年05月25日 ⁄ 综合 ⁄ 共 444字 ⁄ 字号 评论关闭

    题目URL:http://acm.hdu.edu.cn/showproblem.php?pid=1029;

    题目大意是找出数组中出现次数超过一半的数。基本思想是每遇到两个不同的数就消掉,设一个计数器就行了。存出现次数最大的那个数的出现次数。当下一个数与当前的数不同时,计数器减一,相同,则加一。

    贴个AC代码:

#include<iostream>
#include<stdio.h>
using namespace std;

int main()
{
	int cnt, max, cur, n;
	while(scanf("%d", &n) != EOF)
	{
		cnt = 0;
		for(int i=0; i<n; i++)
		{
			scanf("%d", &cur);
			if(!cnt)
			{
				max = cur;
				cnt ++;
			}
			else if(cur != max) cnt --;
			else if(cur == max) cnt ++;
		}
		//cout << max << endl;
		printf("%d\n", max);
	}
	system("pause");
	return 0;
}

抱歉!评论已关闭.