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

打表~1610MS水过,大牛指点,时间也忒久了~

Posted by howling at 2011-07-24 19:00:20 on Problem 3518
#include<iostream>
#include<cmath>
using namespace std;
int map[100000];
int isPrime(int n){
    if(n==0||n==1)return 0;
    if(n==2)return 1;
    for(int i=2;i<=sqrt(n*1.0);i++){
        if(n%i==0)return 0;
    }
    return 1;
    
}
void cal(){
    int count=0;
    for(int i=2;i<=1299709;i++){
        if(isPrime(i)){
            map[count]=i;
            count++;
        }
    }
}
int find_m(int m){
    for(int i=0;i<100000;i++){
        if(m==map[i])return 1;
    }
    return 0;
}
int find(int n){
    int k;
    for(int i=0;i<100000;i++){
        if(map[i]>n){
            k=i;
            break;
        }
    }
    return k;
}
int main (){
    int n;
    cal();
    cin>>n;
    while(n!=0){
        if(find_m(n)==1)cout<<0<<endl;
        else{
            int temp=find(n);
            cout<<map[temp]-map[temp-1]<<endl;
        }
        cin>>n;
    }
   // system("pause");
    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