题解 | 质数因子
质数因子
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); List<Integer> result = new ArrayList<>(); int n = in.nextInt(); while (n % 2 == 0) { result.add(2); n /= 2; } for (int i = 3; i <= Math.sqrt(n); i += 2) { while (n % i == 0) { result.add(i); n /= i; } } if (n > 2) { result.add(n); } System.out.printf(String.join(" ",result.stream().map(Object::toString).toArray(String[]::new))); } }
从2开始判断,判断到输入的平方根即可
同时只按上述输出即可