一.题目描述
Factorization(分解质因数)
Input an integer, then output the factorization result of the integer.
样例输入:
24
样例输出:
24=2x2x2x3
二.参考代码
我们先设定i为最小的质数2开始,从2开始让n除,一直除到n没法继续除为止,边除边输出,再换下一个。我们直接i++就行不用特意判断下一个数是不是质数,因为每个质因子都一直除,直到不能再除,以后也不会出现可以除的数是此数的倍数了,相当于埃式筛法。
#include<bits/stdc++.h>
using namespace std;
int n;
int main ()
{
int i=2;
cin>>n;
cout<<n<<"=";
do {
while(n%i==0)
{
cout<<i;
n/=i;
if(n!=1) cout<<"x";
}
i++;
}while(n!=1);
return 0;
}