题解 | 质数因子
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int s = sc.nextInt(); StringBuilder bu = new StringBuilder(); for (int i = 2; i <= Math.sqrt(s); i++) {//最大不超过平方根,超过了质因数是自身 while (s % i == 0) {//短除法,从2开始取余 bu.append(i + " ");//可以被整除,是一个质因数 s = s / i;//每找到一个质因数更新被除数 } } if (s != 1) { //剩余的数无法被整除,质因数是本身 bu.append(s + " "); } System.out.println(bu.substring(0, bu.length() - 1)); } }