【问题标题】:Finding the primary numbers in an array of pointer在指针数组中查找主数
【发布时间】:2016-12-03 08:40:36
【问题描述】:

这段代码运行良好,它告诉你输入一个数字,然后输入 for 循环中的数字,它检查它是否可以被 i 整除,如果为真,则打印非素数,如果不打印素数。

#include <iostream>
using namespace std;

int main() {
  int x;
  cin >> x;

  bool f = true;
  for (int i = 2; i < x; i++) {
    f = false;
    if (i % x == 0)
      f = true;
    if (f)
      cout << "not primary";
    else
      cout << "primary";
  }
}

但是当我将它转换为这样的数组时:

#include <iostream>
using namespace std;

int main() {
  cout << "the number of array:" << endl;
  int n;
  cin >> n;

  cout << "enter them = \n";
  int *p = new int[n];
  for (int i = 0; i < n; i++)
    cin >> p[i];

  bool f = true;
  for (int i = 0; i < n; i++)
    for (int j = 2; j < p[i]; j++) {
      f = false;
      if (p[i] % j == 0)
        f = true;
      if (f)
        cout << "This is not a primary number!\n";
      else
        cout << "this is a primary number!\n";
    }

  delete p;
}

它只存储第一个数字,我明白了,但是如何增加它 假设 n =3 所以 p[3] = {4,6,7}; 我的问题是如何在 j 条件下告诉编译器 if (p[0] % j) then(p[1] %j) 似乎只需要 p[0]

【问题讨论】:

  • 请告诉我为什么你投了反对票,这样我就可以修复它。如果我的问题很愚蠢,我只是 C++ 的新手。
  • 我没有投反对票,但确实不清楚你在问什么。代码太多,没有足够的精力来查明问题。调试器在这里可能很方便。顺便说一句,它不是“主要”数字,而是“主要”数字。
  • @Jean-FrançoisFabre 我会尝试进一步解释谢谢。
  • 我不明白你的问题。
  • 这段代码运行良好....我敢打赌你有一台量子计算机!不,我的朋友,第一段代码也是完全错误的。

标签: c++ primes


【解决方案1】:

这样会好很多

#include <iostream>
using namespace std;

int main() {
  cout << "the number of array:" << endl;
  int n;
  cin >> n;

  cout << "enter them = \n";
  int *p = new int[n];
  for (int i = 0; i < n; i++)
    cin >> p[i];

  for (int i = 0; i < n; i++) {
    bool f = false; // we set f to false for each number
    for (int j = 2; j < p[i]; j++) {
      if (p[i] % j == 0) {
        f = true;
        break; // we break the loop if it's a prime number
      }
    }
    if (f)
      cout << p[i] << " is not a primary number!\n";
    else
      cout << p[i] << " is a primary number!\n";
  }

  delete[] p; // Here you forget brackets [], when you use new[] you must use delete[].
}

Doc 用于删除运算符。

我让一些问题像int。您不应该使用带符号的数字进行迭代或存储尺寸。因为你是初学者,我不想混淆你。所以我让它。

【讨论】:

  • 带符号的数字是表示大小的正确选择,因为它们的算术以预期的方式工作。无符号数不能防止由负值引起的错误,它们只会让它们更难找到,因为你会默默地得到一个巨大的正值。有关该主题的讨论,请参阅stackoverflow.com/questions/10168079/why-is-size-t-unsigned。虽然最后的delete[] 是善意的,但最好教std::vector。你永远不应该使用new[]
  • @ChristianHackl 我真的不同意。 C++ 可以是一种系统语言。你可以使用新的[]。我同意矢量将是一个不错的选择,但 OP 显然是初学者。我认为最好让答案尽可能接近他的尝试。我不同意签名号码,你有链接的帖子提供了两个答案。我是第二个
  • 该语言的所有高级功能,如std::vectorstd::unique_ptr,都是根据placement new 或new 实现的。你不需要new[],系统编程与否。虽然您可能不同意关于unsigned 的答案,但请知道您也不同意一些世界上最著名的 C++ 专家,包括 Bjarne Stroustrup、Herb Sutter 和 Scott Meyers。具有讽刺意味的是,您的回答给人的印象是使用int 作为尺寸是初学者的错误。大多数有经验的 C++ 程序员可能会反过来看。
  • 非常感谢它为我省去了很多不必要的头痛。
  • @ChristianHackl 我找到了,this 文章。 “另一方面,如果变量被定义为溢出时回绕,那么编译器必须假设循环可能是无限的(如果 N 为 INT_MAX,则会发生这种情况) - 然后禁用这些重要的循环优化。这尤其会影响 64-位平台,因为很多代码都使用“int”作为归纳变量。”
猜你喜欢
  • 2020-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-14
  • 2016-08-21
  • 2013-01-25
相关资源
最近更新 更多