题解 | #质数因子#

质数因子

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

using System;
using System.Collections.Generic;

public class Program {
    public static void Main() {
        string line;
        while ((line = System.Console.ReadLine ()) !=
                null) { // 注意 while 处理多个 case
            int input = int.Parse(line);
            List<int> factors = new List<int>();
            GetFactors(input, factors);
            System.Console.WriteLine(string.Join(' ', factors));
        }
    }

    public static void GetFactors(int num, List<int> factors) {
        int sqrt = (int)Math.Sqrt(num);
        for (int i = 2; i <= sqrt; i++) {
            if (num % i == 0) {
                factors.Add(i);
                GetFactors(num / i, factors);
                return;
            }
        }

        factors.Add(num);
    }
}

全部评论

相关推荐

04-15 09:59
门头沟学院 C++
yy_11:小公司人家没必要泄密,大公司都是本地部署了
你想吐槽公司的哪些规定
点赞 评论 收藏
分享
今天 15:29
佛山大学 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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