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;
}
感觉也没错呀,还是运行不了,怎么回事,本人是菜鸟,希望哪个大神可以帮忙,谢谢大神了,
[最优解释]
首先,数组的索引是从0开始的,所以要多分配一个空间。
其次,不能申请这么大的数组,栈会溢出的,要用malloc
然后,hash[999999]={0};是不对的,要想清空全部值,需用memset或其它方法。
最后,if语句后面还有一对花括号忘了。
[其他解释]
LZ 设999999是不对的
题目是规定n的范围是999999,而不是数组里的数字范围是999999
[其他解释]
#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;
}
[其他解释]
5L正解,这貌似是某知名公司以前的面试题,编程之美还是什么上面有
[其他解释]
没运行过,只是手改了下,呵呵,没注意,的确是溢出了.
[其他解释]
好细心。奖励美女一枚