题解 | 矩形游戏

矩形游戏

https://www.nowcoder.com/practice/5b6c2c824a434b55a5e3b77619c54a90

找到最大的质因子。可以由最小因子除得到

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n=in.nextInt();
        long res=n;
        while(true){
            int a=findSmallestPrimeFactor(n);
            n/=a;
            if(n==1){
                res+=1;
                break;
            }else{
                res+=n;
            }


        }
        System.out.println(res);


    }
    public static int findSmallestPrimeFactor(int a){
        if(a%2==0){
            return 2;

        }
        for(int i=3;i*i<=a;i++){
            if(a%i==0){
                return i;
            }
        }
        return a;

    }
}

全部评论

相关推荐

11-04 10:30
已编辑
门头沟学院 研发工程师
开心小狗🐶:“直接说答案”
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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