3498. 分解质因数
Constraints
Time Limit: 1 secs, Memory Limit: 256 MB
Description
对于输入的整数n,将其分解成若干个素数的乘积.
Input
第一行是一个整数T,表示总共有T组数据.
接下来的T行,每行有一个整数n(2<=n<=1000000).
Output
对于每行的整数n,按从小到大的顺序输出素因子,每个素因子后面有一个空格.
Sample Input
3
100
5
34
Sample Output
2 2 5 5
5
2 17
Problem Source
林瀚
// Problem#: 3498
// Submission#: 1985898
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include<cmath>
int judge(int n);
int main(){
int n,i;
scanf("%d",&i);
while(i--){
scanf("%d",&n);
int i;
int counter=0;
for(i=1;i<=n;i++){
if((n%i)==0){
if(judge(i)==1){
n/=i;
i=0;
}
}
}
printf("\n");
}
return 0;
}
int judge(int n){
if(n==0||n==1){
return 0;
}
else if(n==2||n==3){
printf("%d ",n);
return 1;
}
else{
int temp,i;
temp=sqrt(n);
for(i=2;i<=temp;i++){
if((n%i)==0){
return 0;
}
}
printf("%d ",n);
return 1;
}
}