码迷,mamicode.com
首页 > 其他好文 > 详细

FJUT OJ 2121 唯一分解定理

时间:2018-02-12 23:02:54      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:amp   ret   des   log   它的   int   合数   cst   using   

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;
}

 

FJUT OJ 2121 唯一分解定理

标签:amp   ret   des   log   它的   int   合数   cst   using   

原文地址:https://www.cnblogs.com/a249189046/p/8445723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!