题解 | #质数因子#
质数因子
https://www.nowcoder.com/practice/196534628ca6490ebce2e336b47b3607
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int i = in.nextInt(); List<Integer> set = digui(i, new ArrayList<>()); Collections.sort(set); StringBuffer sbr = new StringBuffer(); for (Integer integer : set) { sbr.append(integer).append(" "); } System.out.println(sbr.toString().substring(0, sbr.length() - 1)); } } private static List<Integer> digui(Integer i, List<Integer> out) { double sqrt = Math.sqrt(i); boolean flag = false; for (int j = 2; j <= sqrt; j++) { if (i % j == 0) { out.add(j); i = i / j; flag = true; break; } } if (flag) { digui(i, out); } else { out.add(i); } return out; } }