需要帮助查找Prime Emirp C++中的错误
Need help finding error in Prime Emirp C++
嘿,我一直在试图找出这段代码中的错误。我应该向用户询问一个正整数,然后在每行中找出第一个emirps 5。。。我在这一点上完全陷入了困境。。感谢
#include <iostream>
using namespace std;
int isPrime(int value); //Prototyle for "prime number function"
int reverse (int value2); //Prototype for "emirp function"
int main()
{
//Ask the user for a positive number
cout << "Please enter a positive number: ";
int n;
cin >> n;
//Reject negative value input
if ( n < 1)
{
cout << "INVALID NUMBER n";
}
else
//Calculate all emirps up to 'n'.
for (int test = 0; test < n; test++)
{
int number = 2;
if (isPrime(number))
{
cout << "n" << reverse(number) << "ttt";
}
}
return 0;
}
int isPrime(int value)
{
//If value is prime, the remainder (count) will be zero twice--for 1 and itself.
int divisor = 1;
int count = 0;
int prime = 0;
if (value % divisor == 0)
{
count++;
++divisor;
}
if ((count = 2))
{
int prime = value; //store prime value in new variable
}
return prime;
}
int reverse(int value2)
{
//reverse the number
value2*=10;
value2 = value2 %10;
value2/=10;
//same procedure as prime function
int divisor2 = 1;
int count2 = 0;
int emirp = 0;
if (value2 % divisor2 == 0)
{//if
count2++;
++divisor2;
}
if ((count2 = 2))
{
int emirp = value2;
}
return emirp;
system ("pause");
请花一些时间正确描述您的问题。我的灵力告诉我,用户输入一个数字,然后程序会打印所有素数,直到这个数字,数字颠倒。(一些赌客选择将数字颠倒的素数称为Emirp。)
嘿,我一直在努力找出这段代码中的错误。。。
嘿,这里没有一个错误。代码中错误百出!
#include <iostream>
using namespace std;
int isPrime(int value);
int reverse (int value2);
int main()
{
cout << "Please enter a positive number: ";
int n;
cin >> n;
if ( n < 1)
{
cout << "INVALID NUMBER n";
}
else
//Calculate all emirps up to 'n'.
for (int test = 0; test < n; test++) ## No need to test 0 or 1 for primality
{
int number = 2;
if (isPrime(number)) ## You always test whether 2 is a prime here
{
cout << "n" << reverse(number) << "ttt";
}
}
return 0;
}
int isPrime(int value)
{
//If value is prime, the remainder (count) will be zero twice--for 1 and itself.
int divisor = 1;
int count = 0;
int prime = 0; ## (A)
if (value % divisor == 0)
{
count++; ## This statelment will be executed at most once
## You should count divisors in a loop
++divisor; ## Here, divisor is incremented, but never used again
## Also, if this were inside a loop, you should increment
## the divisor unconsitionally. The condition affects
## only the count.
}
if ((count = 2)) ## This will set count to 2 and always evaluate to true
{
int prime = value; ## This variable will shadow the "prime" variable
## decralered at (A). This variable will cease to exist
## as soon as the block closes, i.e. immedietely.
## You just want "prime = 1", without the "int", which
## declares a new variable.
}
return prime; ## This prime is tze one declared at (A) and will be 0.
}
int reverse(int value2)
{
value2*=10;
value2 = value2 %10; ## The remainder of a division by 10 of a number that
## you have just multiplied by 10 is 0, rendering pretty
## much the rest of the function useless ...
value2/=10;
int divisor2 = 1; ## ... which doesn't hurt, because the rest of the
## function was useless to start with. Reversing the
## digits of a number isn't at all like finding a prime.
int count2 = 0;
int emirp = 0;
if (value2 % divisor2 == 0)
{
count2++;
++divisor2;
}
if ((count2 = 2))
{
int emirp = value2;
}
return emirp;
system ("pause"); ## First, this statement comes directly after a terurn,
## so it will never be reached. Second, if anywhere, it
## should go into the main routine. You don't want the
## user to press a key before printing each number.
}
请学习:
- 如何使用调试器逐步完成prigram,以了解te变量是如何变化的,以及程序实际做了什么
- 循环和范围块(大括号)的工作方式
- 何时声明新变量,何时使用现有变量;(你想要后者的次数会比你想象的要多)
- 为了更好地组织程序,它将帮助您发现逻辑错误
至于你手头的问题:有足够的资源来测试素数和反转SO上的数字,这应该不难找到。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'