题解 | #质数因子#

质数因子

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());
    }
}
全部评论

相关推荐

05-30 12:03
山西大学 C++
offer来了我跪着...:不是骗子,等到测评那一步就知道为啥这么高工资了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务