题解 | #求最小公倍数#

求最小公倍数

https://www.nowcoder.com/practice/22948c2cad484e0291350abad86136c3

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String[] strings = in.nextLine().split(" ");
        Node node = new Node(Integer.parseInt(strings[0]),
                             Integer.parseInt(strings[1]));
        int result = 1;
        while (!isDoublePrime(node)) {
            result *= node.num;
            node.x1 /= node.num;
            node.x2 /= node.num;
        }
        System.out.println(result * node.x1 * node.x2);
    }

    public static Boolean isDoublePrime(Node node) {
        for (int i = Math.min(node.x1, node.x2); i > 1; i--)
            if (node.x1 % i == 0 && node.x2 % i == 0) {
                node.num = i;
                return false;
            }
        return true;
    }
}

class Node {
    int x1;
    int x2;
    int num = 1;    // 公因数
    public Node(int x1, int x2) {
        this.x1 = x1;
        this.x2 = x2;
    }
}

全部评论

相关推荐

xdm怎么说 要被拷打了 担心是KPI
丹田:面就完了,就当日薪四位数的大佬免费给给你面试。
点赞 评论 收藏
分享
06-26 10:08
门头沟学院 C++
北京Golang实习,一个月4700,吃住都不报,公司位置在海淀。请问友友怎么看呢?如果要租房的话有什么建议吗
码农索隆:租房肯定是合租了,剩下的钱,差不多够正常吃饭了,看看能不能学到东西吧
点赞 评论 收藏
分享
06-10 21:15
门头沟学院 Java
宁阿:好多这种没🧠的公司,他们估计都不知道毕业的人不能给安排实习岗
实习吐槽大会
点赞 评论 收藏
分享
05-19 19:57
蚌埠学院 Python
2237:Gpa70不算高,建议只写排名,个人技能不在多而在精,缩到8条以内。项目留一个含金量高的,减少间距弄到一页,硕士简历也就一页,本科不要写很多
实习,投递多份简历没人回...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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