/*
* 因子分解2<=n<=100
*/
// 输出该整数的因子分解表达式
//表达式中各因子从小到大排列
//如果该整数可以分解成a的b次方,直接写成a^ b; 当b等于1时,直接写a
#include<iostream>
using namespace std;
bool flag = false;//第一次打印不输出‘*’
void f(int n, int div/*因子*/, int dep) {
if (n == 1) {
if (flag) cout << '*';
if (dep >= 2)cout << div << '^' << dep;
else cout << div;
return;
}
if (n % div == 0)//当前div是n的因子
f(n / div, div, dep+1);//搜索下一层div是否仍然为n的因子
else {//当前div不是n的因子
if (dep > 0) {//打印当前分解的因子
if(flag) cout << '*';//第一次打印不输出‘*’
flag = true;
if (dep >= 2)cout << div << '^' << dep;//如果该因子可以用于多次分解
else cout << div;//如果该因子只用于一次分解
}
f(n, div + 1, 0);//换因子就要从头开始计数
}
}
int main()
{
int n;
cin >> n;
f(n, 2, 0);//n从2开始分解
return 0;
}
输入
60
输出
2^2*3*5