题解 | #质数因子#
质数因子
http://www.nowcoder.com/practice/196534628ca6490ebce2e336b47b3607
//输入一个正整数,按照从小到大的顺序输出它的所有质因子(重复的也要列举)(如180的质因子为2 2 3 3 5 )
import java.util.ArrayList;
import java.util.Scanner;public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) { // 注意 while 处理多个 case
int num = in.nextInt();
Main j = new Main();
j.search(num);
}//while
}//main
public void search(int num) {//找因子
int i = 2;
while(num!=1 && i<=num/2) {
if( num%i == 0 ) {
System.out.print(i+" ");//把所有因子i都打印出来,再进行下一步
num = num/i;
}
else {i++; }
}//while
System.out.print(num+" ");
}//search
}