题解 | #质数因子#
质数因子
https://www.nowcoder.com/practice/196534628ca6490ebce2e336b47b3607
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); long longNum = in.nextLong(); int i = 2; // 注意 hasNext 和 hasNextLine 的区别 while (i <= longNum) { // 注意 while 处理多个 case if (longNum % i == 0) { System.out.print(i + " "); if (longNum == i) { return; } longNum = longNum / i; // 判断新longNum是否是素数 boolean flag = true; for (int j = 2; j <= Math.sqrt(longNum); ++j) { if (longNum % j == 0) { flag = false; break; } } if (flag) { System.out.print(longNum); return; } i = 2; } else { i++; } } } }