质数因子

质数因子

http://www.nowcoder.com/questionTerminal/196534628ca6490ebce2e336b47b3607

import java.io.*;
import java.util.*;

public class Main{
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
       while(sc.hasNextLong()){
            long s = sc.nextLong();
           for (int i = 2; i <= Math.sqrt(s); i++) {
                if (s % i == 0) {
                    System.out.print(i + " ");
                    s = s / i;
                    // 还需要进行 i 判断, i -- 和 i++ 进行抵消,保证为 i 
                    i--;
                }
            }
            System.out.println(s + " ");
       }
    }
}
全部评论
这个可以 重点1.i <= Math.sqrt(s) 2.已经是最质因子的不需要再判断,这样就不会重复去计算
点赞 回复 分享
发布于 2021-09-17 20:51

相关推荐

点赞 评论 收藏
分享
评论
1
3
分享

创作者周榜

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