题解 | #质数因子#
质数因子
http://www.nowcoder.com/practice/196534628ca6490ebce2e336b47b3607
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
int number = Integer.valueOf(str.trim());
if (number < 4) {
System.out.println(number);
return;
}
ArrayList<Integer> ans = new ArrayList<>();
for (int i = 2; i <= (int) Math.sqrt(number); i++) {
while (number % i == 0) {
number /= i;
ans.add(i);
}
}
if (number != 1) {
ans.add(number);
}
for (int i = 0; i < ans.size(); i++) {
if (i == ans.size() - 1) {
System.out.print(ans.get(i));
return;
}
System.out.print(ans.get(i) + " ");
}
}
}