题解 | #求最小公倍数#

求最小公倍数

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;
    }
}

全部评论

相关推荐

冰皮月饼_FLORRIEEE:你是准备投产品嘛?可以重新整理一下实习的bulletpoint,侧重描述你的工作所带来的结果收益,不要只写泛泛的内容(比如改写通过xx数据分析,提升xx),产品的价值并不在处理和分析数据的过程
点赞 评论 收藏
分享
Aki-Tomoya:窝趣,人家这是先富带动后富,共同富裕了属于是
投递英伟达等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务