hdu 1029

来源:互联网 发布:深圳软件外包公司排名 编辑:程序博客网 时间:2024/05/05 18:22

题意:找一群数字里出现了至少一半的数字

方法:设置candidate和vote变量

设置第一个数为candidate,以后的数与candidate相同则vote+1 不同则-1;若vote=0,则candidate换为当前所存的temp;当循环一遍之后,candidate为所求。

#include<iostream>

using namespace std;
int main()
{
int candidate,vote,temp,n;
while (cin>>n)
{
vote=0;
while (n--)
{
cin>>temp;
if (vote==0) candidate=temp;
if (candidate==temp)
vote++;
else
vote--;
}
cout<<candidate<<endl;
}
return 0;
}
0 0
原创粉丝点击