gpt4 book ai didi

c++ - 素数查找器找不到素数,在 7 后停止

转载 作者:太空狗 更新时间:2023-10-29 12:31:19 26 4
gpt4 key购买 nike

所以我为 3 到 200 之间的数字做了一个简单的素数查找器。它必须使用一个 bool 变量,仅供引用。没有错误发生。输出是:

The prime numbers between 3 and 200 are:
3
5
7

为什么不继续?我在纸上画了一遍又一遍,找不到我的逻辑错误。

此外;我手写了这个,因为我不知道如何获取我的文件的内容。它存在于我没有根访问权限的远程主机上。有没有更好的方法来复制文件?

#include <iostream>
using namespace std;

int main()
{
int count=0;
cout<<"The prime numbers between 3 and 200 are: "<<endl;

for (int i=3;i<=200;i++)
{
for (int j=2;j<i;j++)
{
bool ptest=i%j;
if (!ptest)
{
break;
}
else if (ptest)
{
count=count+1;
if (count==(i-2))
cout<<i<<endl;
}
}
}
}

最佳答案

j 循环中使用后,您忘记将 count 设置回 0。移动线:

int count = 0;

在第一个 for 循环中。然后你的程序就可以正常工作了(虽然正如 msw 指出的那样,这不是最有效的技术!)

关于c++ - 素数查找器找不到素数,在 7 后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26209470/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com