题解 | #质数因子#

质数因子

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);
        int input = in.nextInt();
        //一个数的因子是看能分解成那些数相乘,然后找出因子中的质数
        //2*90 //2*2*45 //2*2*9*5 //2*2*3*3*5
        List<Integer> res = new ArrayList<>();
        int count = (int) Math.sqrt(input) + 1;
        int i =2;
        while(i<= count){
            if(input%i == 0){
                res.add(i);
                input /= i;
            }else{
                i = i+1;
            }
        }
        if(input != 1) res.add(input);
        res.forEach(t -> System.out.print(t + " "));
    }
}

核心在于这个核心的处理,一般开个平方的数,如果到这个数还无法被整除,那么就是一个质数了

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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