在 C++ 中查找数字因子,但我想在第一行打印因子数
Finding factors of number in C++ but I want to print number of factors on first line
I am able to find the factors but not getting how to print the count of factors like in Sample output in c++
问题:给你一个数N,求出N的所有不同因式
输入: 第一行将包含数字 N.
输出: 在第一行打印 N 的不同因子数。 在第二行中,按 space.
分隔的升序打印所有不同的因素约束条件 1≤N≤10^6
示例输入 1:
4
示例输出 1:
3 //因子个数
1 2 4 //因素
//My code
#include<bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int count =0;
for(int i=1;i<=n;i++){
if(n%i == 0){
count++;
cout<<count<<endl;
cout<<i<<" ";
}
}
将因子存储在向量中:
#include <iostream>
#include <vector>
int main() {
int n;
std::cout << "Please enter a number" << std::endl;
std::cin >> n;
std::vector<int> factors;
for(int i = 1; i <= n; ++i) {
if(n%i == 0) {
factors.push_back(i);
}
}
std::cout << factors.size() << std::endl;
for (std::vector<int>::iterator itr = factors.begin(); itr != factors.end(); ++itr) {
std::cout << *itr << " ";
}
}
请尝试以下代码
int main(){
int n;
cout<<"Enter Number (between 1 and 100,000,00) : ";
cin>>n;
cout<<"Factors\n-------\n";
int count =0;
for(int i=1;i<=n;i++)
{
if(n%i == 0)
{ cout<<i<<" ";
count++;
}
}cout<<"\nNumber of Factors : "<<count<<endl;
return 0;
}