FJUT OJ 2121 唯一分解定理
阿新 • • 發佈:2018-02-13
out spa sync for 素數 可能 bsp while clas
Problem Description
實現整數的唯一分解
Input多組測試數據
每組數據輸入一個整數n(2<=n<=1012)
Output每組數據輸出一行,從小到大輸出n的唯一分解。
SampleInput10 7 24SampleOutput
2 5 7 2 2 2 3
代碼如下:
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; typedeflong 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 唯一分解定理