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

100题 纪念一下! 虽然这题是水题

Posted by szy532164656 at 2013-01-25 15:05:56 on Problem 3518 and last updated at 2013-01-25 15:06:57
#include <iostream>
#include <cmath>

using namespace std;

int ifPrime[1299710];

int main()
{
	for ( int i = 0 ; i <= 1299709 ; i ++ )
		ifPrime[i] = true;
	ifPrime[0] = false;
	ifPrime[1] = false;
	for ( int i = 2 ; i <= sqrt((double)1299710) ; i ++ )
	{
		if ( ifPrime[i] )
			for ( int j = 2 ; i * j <= 1299709 ; j ++ )
				ifPrime[i*j] = false;
	}
	int number;
	while ( true )
	{
		cin>>number;
		if ( number == 0 )
			break;
		int left, right;
		for ( int i = number ; i > 1 ; i -- )
		{
			if ( ifPrime[i] )
			{
				left = i;
				break;
			}
		}
		for ( int i = number ; i <= 1299709 ; i ++ )
		{
			if ( ifPrime[i] )
			{
				right = i;
				break;
			}
		}
		cout<<right-left<<endl;
	}
}

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