描述 输入一个正整数数n,输出这个数的所有质因子[从小到大并用空格隔开] 备注:一个质数的质因子是其本身(注意相同的质因数只会输出一次 例如输入100,只会输出2 5) 输入 一行,一个正整数n(2<=n<=10000)。 输出 一行,n的所有质因子[从小到大并用空格隔开] 输入样例 1 105 输出样例1 3 5 7 c++代码
时间: 2024-04-12 20:26:53 浏览: 93
C++初学者之根据输入的任何一个正整数,输出可能被表示的连续正整数
5星 · 资源好评率100%
Sure! Here's a C++ code that takes a positive integer 'n' as input and outputs all its prime factors in ascending order, separated by a space.
```cpp
#include <iostream>
#include <vector>
using namespace std;
vector<int> getPrimeFactors(int n) {
vector<int> factors;
for (int i = 2; i <= n; i++) {
while (n % i == 0) {
factors.push_back(i);
n /= i;
}
}
return factors;
}
int main() {
int n;
cin >> n;
vector<int> primeFactors = getPrimeFactors(n);
for (int i = 0; i < primeFactors.size(); i++) {
cout << primeFactors[i] << " ";
}
return 0;
}
```
You can use this code to solve the given problem. Let me know if you have any further questions!
阅读全文