【问题标题】:C++ Compute prime factors of a number and print themC++ 计算一个数的质因数并打印出来
【发布时间】:2016-02-10 01:32:30
【问题描述】:

我想向用户询问一个整数,并打印出质因数。 示例:用户输入 100,程序显示 2 2 5 5

到目前为止,我有以下内容:

#include <iostream>

using namespace std;

void factors(int n){

int z = 2;

while (z * z <= n)
{
    if (n % z == 0)
    {   
        cout << z;
        n = (n / z);
    }
    else
    {
        z++;
    }
}
if (n > 1)
    {cout << n << "\n";}
}
int main()
{

int x;

cout << "Input positive integer greater than 1: ";

cin >> x;

factors(x);
cout << "The result: " << x;
return 0;}

我的问题是如何让我的主要功能与因素程序进行通信。我运行程序,我收到要求输入的消息,我输入 12,我收到消息“结果”,但数字是 25,还有 12,用户输入的数字。就像程序正在避免我的因素(int n)程序。请帮助语法?!?

我的问题在于我认为的语法。 因为我发现以下函数可以帮助列出主要因素: Finding prime factors -user44810

define factors(n)

z = 2

while (z * z <= n)

    if (n % z == 0)
        output z
        n /= z

    else
        z++

if n > 1
    output n

谢谢!!!

【问题讨论】:

  • 不要为不同的、不相关的语言添加标签!并正式提出您的问题!。
  • 您之前找到的代码是伪代码,这意味着它不一定遵循任何语言的正确语法,只是为了帮助理解代码应该遵循的过程。
  • 谢谢,我对伪代码的解释在程序的顶部。但是,它不会返回正确的结果。
  • @IglooJits 请注意,您不得篡改自己的问题。发布时,您根据 CC BY SE 许可将其不可撤销地授权给 Stack Exchange。进一步尝试破坏您自己的问题可能会导致版主锁定它。
  • 源格式很差。如果我在给你的作品打分,我会给它“F-”,不打扰测试/进一步阅读,然后继续下一次提交。

标签: c++


【解决方案1】:

试试这个

#include <iostream>
#include <vector>

using namespace std;

vector<int> factors(int n){

vector<int> result;
int z = 2;

while (z * z <= n)
{
    if (n % z == 0)
    {   
        result.push_back(z);
        n = (n / z);
    }
    else
    {
        z++;
    }
}
if (n > 1)
    result.push_back(z);
return result;
}

int main()
{

int x;

cout << "Input positive integer greater than 1: ";

cin >> x;

vector<int> result_factors = factors(x);
cout << "The result: ";
for (int i: result_factors)
  cout << "i  ";
cout << endl;

return 0;
}

我将您的 factor() 函数更改为在 cout 上不输出任何内容,但将因子保存在向量中并返回它。在主函数中,遍历结果向量并将值打印到cout

【讨论】:

  • 感谢您的帮助。我需要先阅读矢量标题,这是我第一次遇到它!到目前为止,我在 CSE 201 课程中只学习过函数和循环。
【解决方案2】:

所以最大的问题是你的 factor 函数末尾缺少一个大括号。您需要在 if (n > 1) 大括号之后添加另一个大括号。此外,最后一个 cout 末尾缺少一个分号,会引发错误。

另一个不会阻止代码运行的问题是,当您打印“结果:”

如果您希望您的主函数在该位置打印出 factor() 的结果,则该函数需要返回数据而不是打印它。为了解决这个问题,可以让 factor 函数返回一个带有你想要的输出的字符串:

//return a string with the output
string factors(int n){

//create a string to save the output to
string factorlist = "";
int z = 2;

while (z * z <= n)
{
    if (n % z == 0)
    {   
        //append z to the end of the string and add a space to make it easier to read
        factorlist+=to_string(z)+" ";
        n = (n / z);
    }
    else
    {
        z++;
    }
}
if (n > 1)
{
//append n to the end of the string and add a newline
factorlist+=to_string(n)+"\n";
}
//output the string factorlist to wherever the function was called from
return factorlist;
}

然后在看起来像这样的行上:

factors(x);
cout << "The result: " << x

应该是:

cout << "The result: " << factors(x);

目前,您只是打印出用户输入的 x 值。如果你想保存 factor(x) 的值,你需要像这样设置一个等于它的变量:

string FactorsResult = factors(x)

然后打印出来;或者,如上面更正的代码,直接在 cout 语句中打印。

【讨论】:

  • 感谢您帮助我。使用:cout &lt;&lt; "The result: " &lt;&lt; factors(x); 我得到一个错误:no match for "operator
  • 希望这次编辑修复了我之前错过的所有内容
  • 我修复了缺少大括号和缺少分号的问题,现在的问题是我收到一个错误@这部分代码:cout &lt;&lt; "the result: " &lt;&lt; factors(x); 它说“操作符factors(x) 之前的 "
  • 糟糕,这也是我的错。您拥有的函数是一个 void 函数,因此它不会返回任何类型的数据。如果您希望它以这种方式工作,则该函数需要返回一些东西。我将再次编辑我的答案。
  • 我尝试将void 类型更改为int,现在程序运行了,但是当我输入数字12 时,输出为:Input positive integer greater than 1: 12 223 The result: 6295968 我看到质数223 在至少!取得进展...
猜你喜欢
相关资源
最近更新 更多
热门标签