[英]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++;
}
}
}
INPUT 6输入 6
OUTPUT 1 2 3 6 4 OUTPUT 1 2 3 6 4
I want to print the Number of Factors which is 4 in this case Before the Factors.我想在因子之前打印在这种情况下为 4 的因子数。 I did it few days ago but i forget that now please help me..
几天前我做了,但我忘记了,现在请帮助我..
The easiest way to achieve what you want by putting the factors in the array.通过将因子放入数组中来实现您想要的最简单的方法。 like: int k =0;
比如:int k =0; arr[k++] = i;
arr[k++] = i;
now, first print the count and then traverse and print the array.现在,首先打印计数,然后遍历并打印数组。
There is no need to cout on line 12 and it will work fine:).无需在第 12 行 cout,它会正常工作:)。 That is printing the "i" which are the numbers you don't need.
那就是打印你不需要的数字“i”。
cout<<i<<" ";
This line remove it.此行将其删除。
Edit: I reread and found that you wanted to print the factors after it.编辑:我重读并发现你想在它之后打印因子。 You could save it in a vector or an array.
您可以将其保存在向量或数组中。
#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.