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

这题要5000ms干啥??暴力才16ms

Posted by KatrineYang at 2016-11-07 01:36:37 on Problem 3518
#include <stdio.h>
#include <cmath>
bool p(int n){
	if(!(n^2))return 1;
	if(!(n%2))return 0;
	int q = (int)sqrt(n+0.0);
	for(int i=3; i<=q;i+=2){
		if(!(n%i))return 0;
	}
	return 1;
}
int main() {
	int n;
	while(1){
		scanf("%d",&n);
		if(!n)break;
		if(p(n))printf("0\n");
		else{
			int s=n-1,x=n+1;
			for(;!p(s);s--);
			for(;!p(x);x++);
			printf("%d\n",x-s);
		}
	}
	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