Ignatius and the Princess IV
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K (Java/Others)Total Submission(s): 40942 Accepted Submission(s): 17900
"I will tell you an odd number N, and then N integers. There will be a special integer among them, you have to tell me which integer is the special one after I tell you all the integers." feng5166 says.
"But what is the characteristic of the special integer?" Ignatius asks.
"The integer will appear at least (N+1)/2 times. If you can't find the right integer, I will kill the Princess, and you will be my dinner, too. Hahahaha....." feng5166 says.
Can you find the special integer for Ignatius?
求出所有数中出现最多的数。
#include<bits/stdc++.h>
using namespace std;
int n;
int num,cnt,ans;
int main()
{
while(~scanf("%d",&n)){
cnt=0;
for(int i=1;i<=n;i++){
scanf("%d",&num);
if(cnt==0){
ans = num;
cnt = 1;
}
else {
if(num==ans) cnt++;
else cnt--;
}
}
printf("%d\n",ans);
}
return 0;
}
1854




被折叠的 条评论
为什么被折叠?



