题解 | #Prime Number#

Prime Number

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

写于2024.3.23

#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <cmath>
using namespace std;
const int MAXN = 10000;
int a[MAXN]; // 数组a[i]用于保存第i个质数

bool judge(int x) // 判断x是否为质数
{
    if (x < 2)
        return false;
    int bound = sqrt(x);
    for (int i = 2; i <= bound; i++)
    {
        if (x % i == 0)
            return false;
    }
    return true;
}

int main()
{
    int k, i = 0, n = 0;
    cin >> k;
    // cout << k;
    while (k--)
    {
        while (!judge(i)) // 如果判断出来不是素数
        {
            i++;
            // cout << i << endl;
        }
        // cout << "hdu";
        a[n] = i;
        i++;
        n++;
    }
    // cout << k;
    cout << a[n - 1] << endl;

    return 0;
}

全部评论

相关推荐

04-06 16:59
已编辑
河南工业大学 Java
牛牛牛的牛子:最好扔了,实在没有选择的选择
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务