题解 | #HJ006 质数因子#

质数因子

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

import java.util.Scanner;

/**
 * HJ6 质数因子
 */
public class HJ006 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long num = sc.nextLong();
        String result = getResult(num);
        System.out.println(result);
        sc.close();
    }

    /**
     * 获取输入数据的质因数组成的字符串结果
     */
    public static String getResult(long num) {
        String result = "";
        for (long i = 2; i <= Math.sqrt(num); i++) {
            while (num % i == 0) {
                num = num / i;
                result = result + i + " ";
            }
        }
        return num == 1 ? result : result + num + " ";
    }

}
全部评论

相关推荐

找到实习了&nbsp;给了150一天&nbsp;但是说是低代码&nbsp;值得去吗
码农索隆:是在没实习,可去,待个一两周,不行就润呗
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
昨天 11:30
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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