题解 | #质数因子#
质数因子
http://www.nowcoder.com/practice/196534628ca6490ebce2e336b47b3607
思路: 被除数(a)/除数(b)= 整除后的数(c)...余数(d); 当d=0;继续用c/b,直到d!=0时,b自增+1;每除尽一次,将除数b存起来。 为了减少循环,循环的时候要判断除数<被除数(被除数一直在变化,始终为上一次的余数)
import java.util.*;
public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        List<Integer> M = new ArrayList<>();
        int a = n;
        
        for(int i = 2; i <= a;){
            if (a % i == 0){
                M.add(i);
                a = a/i;
            } else {
                i++;
            }
        }
        StringBuilder sb = new StringBuilder();
        for(Integer b : M){
            sb.append(b).append(" ");
        }
        System.out.println(sb.toString().trim());
    }
}
 查看30道真题和解析
查看30道真题和解析