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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值