题解 | #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 + " ";
    }

}
全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务