Fork me on GitHub

题目1040:Prime Number(第k个素数)

题目链接:http://ac.jobdu.com/problem.php?pid=1040

 

详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus

 

参考代码:

//
//  1040 Prime Number.cpp
//  Jobdu
//
//  Created by PengFei_Zheng on 12/04/2017.
//  Copyright © 2017 PengFei_Zheng. All rights reserved.
//
 
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cmath>
#define max_size 1000010
using namespace std;
 
bool prime[max_size];
 
 
void init(){
    memset(prime,1,sizeof(prime));
    prime[0] = false;
    prime[1] = false;
     
    int x = sqrt(max_size) + 1 ;
     
    for(int i = 2; i < x ; i++){
         
        if(prime[i] == true ){
            for(int j = i + i ; j < max_size ; j += i)
                prime[j] = false ;
        }
    }
}
 
int main(){
    init();
    int k;
    while(scanf("%d",&k)!=EOF){
        int nums = 0 ;
        for(int i = 0 ; i < max_size ; i ++){
            if(prime[i] == true){
                nums++;
                if(nums==k){
                    printf("%d\n",i);
                    break;
                }
            }
        }
    }
    return 0;
}
/**************************************************************
    Problem: 1040
    User: zpfbuaa
    Language: C++
    Result: Accepted
    Time:10 ms
    Memory:2496 kb
****************************************************************/

 

posted @ 2017-04-12 21:40  伊甸一点  阅读(506)  评论(0编辑  收藏  举报