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

请教,这道题目是不是应该用筛选法~~~?请高手指点啊!

Posted by derness at 2007-07-27 19:11:55 on Problem 3292
不过我用筛选,得到的结果是错的,找不出问题出在哪里。
#include <iostream>
#include <cmath>
using namespace std;
int main(void)
{
	unsigned int H;
	unsigned long i,j,index;
	unsigned int count;
	int *a=new int[1000002];
	for(i=1;i<1000002;i++)a[i]=1;
	for(i=5;i<1000002;i+=4){
		for(j=5,index=i*j;index<1000002,j<(1000002/i)+1;j+=4)
		{
			index=i*j;
			a[index]=0;
		}
	}
	while(1){
		cin>>H;
		if(H ==0)
			break;
		count=0;
		for(i=5;i<=H;i+=4){
			if(a[i])
				count++;
		}
		cout<<H<<" "<<count<<endl;
	}
	delete[]a;
	return 0;
}

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