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

骗人 1《=N《=1000 竟然有-1

Posted by howling at 2011-07-31 11:27:16 on Problem 1595
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int a[1200];
int cnt=0;
int isPrime(int n){
    if(n==0)return 0;
    if(n==1||n==2)return 1;
    for(int i=2;i<=sqrt(n*1.0);i++){
        if(n%i==0)return 0;
    }
    return 1;
}
void cal(void){
    memset(a,0,sizeof(a));
    
    for(int i=1;i<=1000;i++){
        if(isPrime(i)){
            a[cnt]=i;
            cnt++;
            
            
        }
    }
    
}
int find(int n){
    if(n==0)return -1;//删掉此行 RE
    for(int i=0;i<cnt;i++)
    if(a[i]==n)return i;
    for(int i=0;i<cnt;i++){
        if(a[i]>n)return i-1;
    }
    
}
void solve(int n,int c){
    int t=find(n)+1;
    
    printf("%d %d:",n,c);
    if(t%2==0){
        if(2*c>t){
            
            for(int i=0;i<cnt;i++){
                if(a[i]<=n)cout<<" "<<a[i];
            }
            
        }
        else{
            int temp=(t-2*c)/2;
            int C=2*c;
            while(C--){
                cout<<" "<<a[temp];
                temp++;

            }

        }
        cout<<endl<<endl;
    }
    else if(t%2!=0){
        if(2*c-1>t){

            for(int i=0;i<cnt;i++){
                if(a[i]<=n)cout<<" "<<a[i];
            }

        }
        else{
            int temp=(t-(2*c-1))/2;
            int C=2*c-1;
            while(C--){
                cout<<" "<<a[temp];
                temp++;

            }

        }
        cout<<endl<<endl;
    }
}
int main (){
    int a,b;
    cal();
    while(scanf("%d%d",&a,&b)!=EOF){
        solve(a,b);
    }
    

    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