FJUT OJ 2121 唯一分解定理

Problem Description

实现整数的唯一分解

Input

多组测试数据

每组数据输入一个整数n(2<=n<=1012)

Output

每组数据输出一行,从小到大输出n的唯一分解。

SampleInput
10
7
24
SampleOutput
2 5
7
2 2 2 3

代码如下:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long LL;
int main()
{
     ios::sync_with_stdio(false);
     LL n;
     while(cin>>n)
    {
     for(LL i=2;i*i<=n;i++) 
     {
       while(n%i==0)  //直接遍历分解即可,因为如果是合数的话,一定会更早被它的素数因子分解掉 
       {
            cout<<i<<" ";
            n/=i;
       }
     }
       if(n!=1)cout<<n<<endl;//可能已经分解结束,所以需要判断此时的n是否为1,如果不为1就必为素数 
    }
    return 0;
}
原文地址:https://www.cnblogs.com/a249189046/p/8445723.html