Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:天哪,还有这样的错误!!

Posted by HellHold at 2007-07-27 21:56:37 on Problem 3292
In Reply To:天哪,还有这样的错误!! Posted by:liweishun2269 at 2007-07-27 14:43:54
> #include<iostream>
> #include<cmath>
> using namespace std;
> int hprime[1000000],hsemi[1000000],sum=0;
> int main()
> {
> 	int hnum,i,j,k,count,tt;
> 	for(i=0;i<1000001;i++)hsemi[i]=0;
> 	for(i=5;i<199998;i+=4)
> 	{
> 		k=sqrt(i);
> 		for(j=5;j<=k;j+=4)if(i%j==0)break;
> 		if(j>k)hprime[sum++]=i;
> 	}
> 	for(i=0;i<sum-1;i++)
> 	{
> 问题在这儿:	for(j=0;j<sum;j++)
> 		{
> 			tt=hprime[i]*hprime[j];
> 			if(tt>999985)break;
> 			if(j<i)continue;
> 			hsemi[tt]=1;
> 		}
> 	}
> 	while(1)
> 	{
> 		cin>>hnum;
> 		if(hnum==0)return 0;
> 		count=0;
> 		for(i=21;i<=hnum;i+=4)if(hsemi[i])count++;
> 		cout<<hnum<<" "<<count<<endl;
> 	}
> 	return 0;
> }
> 请高手帮忙看看,我把上面那个地方的循环改成j从i开始,就出现了非常严重的错误,经证实,tt的某些值变成了负数;而当j从0开始时,tt的值一直都很正常,最后结果也对,就是超时!!不明白!!


主要是后面询问得太多,不知道你前面判素数会不会也超掉,建议筛。。。

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator