#include <iostream>
using namespace std;
void fjzys(int n);
int main() { int n; cout << "输入一个数:"; cin >> n; cout << "质因数为:"; fjzys(n); }
void fjzys(int x) { //2,3,5,7 是质数 if(x == 2 || x == 3 || x == 5 || x ==7) cout << x << " "; else {
if(x % 2 == 0)
{
fjzys(x / 2);
cout << 2 << " ";
}
else if(x % 3 == 0)
{
fjzys(x / 3);
cout << 3 << " ";
}
else if(x % 5 == 0)
{
fjzys(x / 5);
cout << 5 << " ";
}
else if(x % 7 == 0)
{
fjzys(x / 7);
cout << 7 << " ";
}
else
//如果无法被 2,3,5,7 整除,则直接判断为质数
cout << x << " ";
}
}
业务学习编程,始终感觉程序写得有点重复,算法不理想。


