【问题标题】:How can I find prime reversed numbers?我怎样才能找到素数反转数?
【发布时间】:2021-01-27 07:15:13
【问题描述】:

我必须编写一个程序来检查输入的数字是否具有这些条件:

一个本身是素数的数字,该数字的反面也是素数,并且该数字的数字也是素数(例如这个数字:7523)。 如果需要,进入运行程序时必须显示“是”,否则为“否”。

我知道素数和倒数的代码,但我不知道如何合并它们。

这是代码:

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

程序在每一步都要检查输入的数字的每个数字是否是质数,然后显示“是”,但它不起作用。

【问题讨论】:

  • 什么在你眼里不起作用?您是否已经使用调试器一步一步地通过您的应用程序?您知道您的应用程序从不调用 prime_check 吗?
  • "[...]但我不知道如何合并它们。"在while循环结束之前调用prime_check(a)
  • 问题是你的每个函数都在做三个的事情,1)输入数字,2)测试数字和3)输出结果。要结合这些功能,您需要有两个仅测试数字的功能。然后您可以在同一个号码上使用这两个功能,而不是输入两个不同的号码。您将需要学习如何使用函数参数,将 inutp 编号传递给两个函数,以及如何使用函数返回值来返回测试结果。数字的输入和结果的输出在main中。
  • 提示:除 2 和 5 外,素数不能以 0、2、4、5、6、8 中的任何一个数字结尾。由于您也在查看反转的数字,因此意味着您无需费心测试以这些数字开头的数字,因为反转的数字将以原始起始数字结尾,因此不能是素数。

标签: c++ reverse primes


【解决方案1】:

欢迎来到本站。

我不知道如何合并它们。

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";

在这里查看一个工作示例:

http://www.cpp.sh/2hpam

请注意,它显示 &lt;stdin&gt;::14 而不是文件名,因为它在网页上运行。

【讨论】:

  • 只需添加打印件,我最终会经常使用它们。无论如何,你不能总是使用调试器。
【解决方案2】:

我对您的代码进行了一些更改,并在我进行更改的所有地方添加了 cmets。看看吧:

#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";

}

【讨论】:

  • 我现在不想使用 bool,因为我们的老师还没有说任何关于它的内容,但谢谢你的提示
【解决方案3】:

假设您有一个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";
}

【讨论】:

  • meta.stackoverflow.com/questions/334822/… 看看这部分告诉如何回答明显的家庭作业问题
  • @0xbaadf00d 我只是想提供一种使用prime_check() 检查所需条件的最佳方法。该函数的定义没有提供给学生,只提示应该返回一个true/false值的事实。来到每个数字的检查,逻辑已经被他/她成功地执行了。此代码不提供任何可能提供直接解决方案的内容,而只是尝试显示运行 main() 函数的最佳方式 (IMO)。所以恕我直言,这符合 SO 准则。
【解决方案4】:

问题在于您的每个函数都在做 件事情,1)输入数字,2)测试数字和 3)输出结果。要组合这些功能,您需要有两个仅测试数字的功能。然后您可以在同一个数字上使用这两个函数,而不是输入两个不同的数字并打印两个不同的结果。您将需要使用函数参数,将输入数字传递给两个函数,并使用函数返回值来返回测试结果。数字的输入和结果的输出在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";
}

我会让你自己编写函数,这里也没有什么可以做数字检查的。我会让你去做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    • 2023-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多