B - Ignatius and the Princess IV
"OK, you are not too bad, em... But you can never pass the next test." feng5166 says.
"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?
5 1 3 2 3 3 11 1 1 1 1 1 5 5 5 5 5 5 7 1 1 1 1 1 1 1
3 5 1
简直水到惨不忍睹
刚接触C语言的时候这道题就能ac了。。不多说直接挂代码
#include <stdio.h>
#include <stdlib.h>
int a[1000005];
int b[1000005]= {0};
int main()
{
int n,j,i=0;
while (scanf("%d",&n)!=EOF&&n!=0)
{
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
j=a[i];
b[j]++;
if (b[j]>=(n+1)/2)
{
printf("%d\n",j);
break;
}
}
for(i=0;i<n;i++)
{
j=a[i];
b[j]=0;
}
}
return 0;
}