hdu1029
http://acm.hdu.edu.cn/showproblem.php?pid=1029
#include <stdio.h>
#include <stdlib.h>
int main()
{//统计数字的最好方法,
int hash[999999];
int i,n,t;
while(scanf("%d",&n)!=EOF)
{
hash[999999]={0};
for(i=0;i<n;i++)
{
scanf("%d",&t);
hash[t]++;
}
for(i=0;;i++)
{
if(hash[t]>=(n+1)/2)
printf("%d\n",t);
break;
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
//统计数字的最好方法
int main(void)
{
int hash[999999] = {0};
int i, n, t;
while (scanf("%d", &n) != EOF)
{
//hash[999999]={0};
for(i = 0; i < n; i++)
{
scanf("%d", &t);
hash[t]++;
}
for (i = 0; ; i++)
{
if (hash[t] >= (n+1)/2)
{
printf("%d\n", t);
break;
}
}
}
return 0;
}
#include <stdio.h>请提交试一下。
#include <stdlib.h>
int main()
{
//统计数字的最好方法,
int * hash;
int i,n,t;
hash = (int*)malloc( 1000000*sizeof(int) );
while(scanf("%d",&n)!=EOF)
{
memset( hash, 0, sizeof(int)*1000000 );
for(i=0;i<n;i++)
{
scanf("%d",&t);
hash[t]++;
}
for(i=0;;i++)
{
if(hash[t]>=(n+1)/2)
{
printf("%d\n",t);
break;
}
}
}
free( hash );
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, n, t;
while( scanf( "%d", &n ) != EOF )
{
int candidate = 0;
int times = 0;
for( i = 0; i < n; ++i )
{
scanf( "%d", &t );
if( times == 0 )
{
candidate = t;
times = 1;
}
else
{
if( candidate == t )
++times;
else
--times;
}
}
printf( "%d\n", candidate );
}
return 0;
}