题解 | #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;
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 11:24
大家还是用ai改吧,我心疼得要死,就当花钱买教训吧,人家直接拿完钱就跑路了
程序员小白条:简历修改700....神奇,又不是帮你面试,咋的,简历修改从双非变92了还是没实习变成有大厂实习了
点赞 评论 收藏
分享
06-04 09:27
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务