题解 | #求最小公倍数#

求最小公倍数

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

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Objects;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String nextLine = in.nextLine();
            if (Objects.isNull(nextLine) || nextLine.equals("")) {
                break;
            }
            String[] s = nextLine.split(" ");
            int n1 = Integer.parseInt(s[0]);
            int n2 = Integer.parseInt(s[1]);
            int min = Math.min(n1, n2);
            // 找出最大公约数
            int max = 1;
            for (int i = 1; i <= n1; i++) {
                if (n1 % i == 0 & n2 % i == 0) {
                    max = i;
                }
            }
            System.out.println(BigDecimal.valueOf(n1)
                .divide(BigDecimal.valueOf(max), RoundingMode.HALF_UP)
                .multiply(BigDecimal.valueOf(n2)));
        }

    }
}

全部评论

相关推荐

双尔:反手回一个很抱歉,经过慎重考虑,您与我的预期暂不匹配,感谢您的投递
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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