题解 | #素数#

Prime Number

http://www.nowcoder.com/practice/c5f8688cea8a4a9a88edbd67d1358415

#include <iostream>
#include <cmath>

using namespace std;
const int N = 1e6+10;


bool st[N];
int primes[N], cnt;
void get_primes(int n)
{
    for (int i = 2; i <= n; i++)
    {
        if (st[i])
            continue;
        primes[cnt++] = i;
        for (int j = i + i; j <= n; j += i)
            st[j] = true;
    }
}

int main()
{
    int k;
    get_primes(N);
    // for(int i = 0; i < 10; i ++) cout << primes[i] << endl;
    while (cin >> k)
    {
        cout << primes[k - 1] << endl;
    }
    return 0;
}
全部评论

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务