繁体   English   中英

如何在没有因子的情况下打印数字的因子数请查看我的代码

[英]How Can I print The number of Factor of a Number without there factors please have a look on my code

#include<iostream>

using namespace std;

int main(){

    int N,i;
    int count=0;
    cin>>N;

    for(i=1;i<=N;i++){
        if(N%i==0){
            cout<<i<<" ";
            count++;
        }
  
    }

}

输入 6

OUTPUT 1 2 3 6 4

我想在因子之前打印在这种情况下为 4 的因子数。 几天前我做了,但我忘记了,现在请帮助我..

通过将因子放入数组中来实现您想要的最简单的方法。 比如:int k =0; arr[k++] = i;

现在,首先打印计数,然后遍历并打印数组。

无需在第 12 行 cout,它会正常工作:)。 那就是打印你不需要的数字“i”。

cout<<i<<" ";

此行将其删除。

编辑:我重读并发现你想在它之后打印因子。 您可以将其保存在向量或数组中。

#include <vector> // At the top of your program
std::vector<int> factors; // At the declaration step
factors.push_back(i); // Where your cout << "i" initially was
// After the cout of your count
for ( auto &factor : factors ){
    std::cout << factor << " ";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM