题解 | #求最小公倍数#

求最小公倍数

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

全部评论

相关推荐

仁者伍敌:服务员还要脱颖而出,这是五星级酒店吗
点赞 评论 收藏
分享
06-12 16:00
天津大学 Java
牛客30236098...:腾讯坏事做尽,终面挂是最破防的 上次被挂了后我连简历都不刷了
点赞 评论 收藏
分享
zhch7:建议9✌️把学历加黑加粗,如果实在offer可能是觉得佬不会去
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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