题解 | #质因数的个数#

质因数的个数

https://www.nowcoder.com/practice/20426b85f7fc4ba8b0844cc04807fbd9

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    static int total = 0;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextInt()) {
            int number = in.nextInt();
            // 递归查询
            int result = divide(number);
            System.out.println(result + 1);
            total = 0;
        }
    }

    static int divide(int num) {
        int nextNum = 0;
        boolean flag = false;
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) {
                total++;
                nextNum = num / i;
                flag = true;
            }
            if (flag) {
                break;
            }
        }
        if (!flag) {
            return total;
        }
        return divide(nextNum);
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 17:10
什么素质,我请问呢,要掉小珍珠了。。。又憋屈又生气
Steven267:这不喷回去?花钱是大爷,记住这个道理
点赞 评论 收藏
分享
面向对象的火龙果很爱...:去吃一顿炸鸡就走
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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