我怎样才能找到质数反转数?
How can I find prime reversed numbers?
我必须编写一个程序来检查输入的号码是否符合这些条件:
A number that is prime it self, the reverse of that number is also prime, and the number's digits are prime numbers too (Like this number: 7523).
If the needs meet, it has to show "yes" when you enter and run the program otherwise "no".
我知道质数和反数的代码,但我不知道如何合并它们。
这是代码:
#include <iostream>
#include <conio.h>
using namespace std;
void prime_check(int x) {
int a, i, flag = 1;
cin >> a;
for (i = 2; i <= a / 2 && flag == 1; i++) {
if (a % i == 0)
flag = 0;
}
if (flag == 1)
cout << "prime";
else
break;
}
int main() {
int a, r, sum = 0;
cin >> a;
while (a != 0) {
r = a % 10;
sum = (sum * 10) + r;
a = a / 10;
}
}
程序每一步都要检查输入数字的每一位是否为质数,然后显示“是”,但它不起作用。
问题是您的每个函数都在做 三项 事情,1) 输入数字,2) 测试数字和 3) 输出结果。要组合这些功能,您需要有两个仅测试数字的功能。然后你可以在同一个数字上使用这两个函数,而不是输入两个不同的数字并打印两个不同的结果。您将需要使用函数参数,将输入数字传递给两个函数,并将函数 return 值传递给 return 测试结果。数字的输入和结果的输出都在main中。这是大纲
// returns true if the number is a prime, false otherwise
bool prime_check(int a)
{
...
}
// returns true if the number is a reverse prime, false otherwise
bool reverse_prime_check(int a)
{
...
}
int main()
{
int a;
cin >> a;
if (prime_check(a) && reverse_prime_check(a))
cout << "prime\n";
else
cout << "not prime\n";
}
我会让你自己编写函数,这里也没有任何东西可以进行数字检查。我会让你做的。
假设您有一个 int
变量 num
要检查您的条件,您可以通过以下方式实现您的目标:
int rev_num = 0;
bool flag = true; // Assuming 'num' satisfies your conditions, until proven otherwise
if (prime_check(num) == false) {
flag = false;
}
else while (num != 0) {
int digit = num % 10;
rev_num = rev_num * 10 + digit;
// Assuming your prime_check function returns 'true' and 'false'
if (prime_check(digit) == false) {
flag = false;
break;
}
num /= 10;
}
if (prime_check(rev_num) == false) {
flag = false;
}
if (flag) {
cout << "Number satisfies all conditions\n";
}
else {
cout << "Number does not satisfy all conditions\n";
}
我对您的代码做了一些修改,并在我修改的地方添加了注释。看看:
#include <iostream>
#include <conio.h>
using namespace std;
bool prime_check(int x) { // I have changed the datatype of this function to bool, because I want to store if all the digits are prime or not
int i, flag = 1; // Removed the variable a, because the function is already taking x as input
for (i = 2; i <= x / 2 && flag == 1; i++) {
if (x % i == 0)
flag = 0;
}
return flag == 1;
}
int main() {
int a, r, sum = 0, original; // added original variable, to store the number added
bool eachDigit = true; // added to keep track of each digit
cin >> a;
original = a;
while (a != 0) {
r = a % 10;
eachDigit = prime_check(r); // Here Each digit of entered number is checked for prime
sum = (sum * 10) + r;
a = a / 10;
}
if (eachDigit && prime_check(original) && prime_check(sum)) // At the end checking if all the digits, entered number and the revered number are prime
cout << "yes";
else
cout<< "no";
}
为了优化,你可以在开始循环之前检查输入的数字是否为素数,如果输入的数字中有一位不是素数,你也可以立即中断循环,像这样:
#include <iostream>
#include <conio.h>
using namespace std;
bool prime_check(int x) { // I have changed the datatype of this function to bool, because I want to store if all the digits are prime or not
int i, flag = 1; // Removed the variable a, because the function is already taking x as input
for (i = 2; i <= x / 2 && flag == 1; i++) {
if (x % i == 0)
flag = 0;
}
return flag == 1;
}
int main() {
int a, r, sum = 0;
bool eachDigit = true, entered; // added to keep track of each digit
cin >> a;
entered = prime_check(a);
while (a != 0 && entered && eachDigit) {
r = a % 10;
eachDigit = prime_check(r); // Here Each digit of entered number is checked for prime
sum = (sum * 10) + r;
a = a / 10;
}
if (eachDigit && entered && prime_check(sum)) // At the end checking if all the digits, entered number and the revered number are prime
cout << "yes";
else
cout<< "no";
}
欢迎来到本站。
I don't know how to merge them.
void prime_check(int n) { /*code*/ }
我知道你不知道如何使用它。
非常简单!
int main()
{
int i = 0;
prime_check(i);
}
如果您对程序的执行方式感到困惑,您可以使用调试器来查看它的执行位置。但是由于一开始使用调试器可能有点困难,我建议添加调试打印以查看程序的执行情况。
这行代码自动打印文件和行号。
std::cout << __FILE__ << ":" << __LINE__ << "\n";
我建议将它添加到您希望了解的每个函数的开头。
更进一步就是把它做成一个宏,方便使用。
#define DEBUGPRINT std::cout << __FILE__ << ":" << __LINE__ << "\n";
在此处检查一个工作示例:
请注意,它说的是 <stdin>::14
而不是文件名,因为它在网页上是 运行。
我必须编写一个程序来检查输入的号码是否符合这些条件:
A number that is prime it self, the reverse of that number is also prime, and the number's digits are prime numbers too (Like this number: 7523). If the needs meet, it has to show "yes" when you enter and run the program otherwise "no".
我知道质数和反数的代码,但我不知道如何合并它们。
这是代码:
#include <iostream>
#include <conio.h>
using namespace std;
void prime_check(int x) {
int a, i, flag = 1;
cin >> a;
for (i = 2; i <= a / 2 && flag == 1; i++) {
if (a % i == 0)
flag = 0;
}
if (flag == 1)
cout << "prime";
else
break;
}
int main() {
int a, r, sum = 0;
cin >> a;
while (a != 0) {
r = a % 10;
sum = (sum * 10) + r;
a = a / 10;
}
}
程序每一步都要检查输入数字的每一位是否为质数,然后显示“是”,但它不起作用。
问题是您的每个函数都在做 三项 事情,1) 输入数字,2) 测试数字和 3) 输出结果。要组合这些功能,您需要有两个仅测试数字的功能。然后你可以在同一个数字上使用这两个函数,而不是输入两个不同的数字并打印两个不同的结果。您将需要使用函数参数,将输入数字传递给两个函数,并将函数 return 值传递给 return 测试结果。数字的输入和结果的输出都在main中。这是大纲
// returns true if the number is a prime, false otherwise
bool prime_check(int a)
{
...
}
// returns true if the number is a reverse prime, false otherwise
bool reverse_prime_check(int a)
{
...
}
int main()
{
int a;
cin >> a;
if (prime_check(a) && reverse_prime_check(a))
cout << "prime\n";
else
cout << "not prime\n";
}
我会让你自己编写函数,这里也没有任何东西可以进行数字检查。我会让你做的。
假设您有一个 int
变量 num
要检查您的条件,您可以通过以下方式实现您的目标:
int rev_num = 0;
bool flag = true; // Assuming 'num' satisfies your conditions, until proven otherwise
if (prime_check(num) == false) {
flag = false;
}
else while (num != 0) {
int digit = num % 10;
rev_num = rev_num * 10 + digit;
// Assuming your prime_check function returns 'true' and 'false'
if (prime_check(digit) == false) {
flag = false;
break;
}
num /= 10;
}
if (prime_check(rev_num) == false) {
flag = false;
}
if (flag) {
cout << "Number satisfies all conditions\n";
}
else {
cout << "Number does not satisfy all conditions\n";
}
我对您的代码做了一些修改,并在我修改的地方添加了注释。看看:
#include <iostream>
#include <conio.h>
using namespace std;
bool prime_check(int x) { // I have changed the datatype of this function to bool, because I want to store if all the digits are prime or not
int i, flag = 1; // Removed the variable a, because the function is already taking x as input
for (i = 2; i <= x / 2 && flag == 1; i++) {
if (x % i == 0)
flag = 0;
}
return flag == 1;
}
int main() {
int a, r, sum = 0, original; // added original variable, to store the number added
bool eachDigit = true; // added to keep track of each digit
cin >> a;
original = a;
while (a != 0) {
r = a % 10;
eachDigit = prime_check(r); // Here Each digit of entered number is checked for prime
sum = (sum * 10) + r;
a = a / 10;
}
if (eachDigit && prime_check(original) && prime_check(sum)) // At the end checking if all the digits, entered number and the revered number are prime
cout << "yes";
else
cout<< "no";
}
为了优化,你可以在开始循环之前检查输入的数字是否为素数,如果输入的数字中有一位不是素数,你也可以立即中断循环,像这样:
#include <iostream>
#include <conio.h>
using namespace std;
bool prime_check(int x) { // I have changed the datatype of this function to bool, because I want to store if all the digits are prime or not
int i, flag = 1; // Removed the variable a, because the function is already taking x as input
for (i = 2; i <= x / 2 && flag == 1; i++) {
if (x % i == 0)
flag = 0;
}
return flag == 1;
}
int main() {
int a, r, sum = 0;
bool eachDigit = true, entered; // added to keep track of each digit
cin >> a;
entered = prime_check(a);
while (a != 0 && entered && eachDigit) {
r = a % 10;
eachDigit = prime_check(r); // Here Each digit of entered number is checked for prime
sum = (sum * 10) + r;
a = a / 10;
}
if (eachDigit && entered && prime_check(sum)) // At the end checking if all the digits, entered number and the revered number are prime
cout << "yes";
else
cout<< "no";
}
欢迎来到本站。
I don't know how to merge them.
void prime_check(int n) { /*code*/ }
我知道你不知道如何使用它。
非常简单!
int main()
{
int i = 0;
prime_check(i);
}
如果您对程序的执行方式感到困惑,您可以使用调试器来查看它的执行位置。但是由于一开始使用调试器可能有点困难,我建议添加调试打印以查看程序的执行情况。
这行代码自动打印文件和行号。
std::cout << __FILE__ << ":" << __LINE__ << "\n";
我建议将它添加到您希望了解的每个函数的开头。
更进一步就是把它做成一个宏,方便使用。
#define DEBUGPRINT std::cout << __FILE__ << ":" << __LINE__ << "\n";
在此处检查一个工作示例:
请注意,它说的是 <stdin>::14
而不是文件名,因为它在网页上是 运行。