题解 | #求最小公倍数#

求最小公倍数

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);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
        // 最简洁的算法:递归
            // Scanner in = new Scanner(System.in);
            int a = in.nextInt();
            int b = in.nextInt();
            System.out.println(getLCM(a,b));
        }
    }
    public static int getGCD(int a,int b) {
        //原理:gcd(a,b)==gcd(b,a%b)
        return b==0?a:getGCD(b ,a%b);
    }
    public static int getLCM(int a,int b)
    {
        return b*a/getGCD(a,b);
    }
}

全部评论

相关推荐

01-04 11:28
已编辑
广州华立学院 Java
程序员花海:实习内容写的看起来太偏向于技术了 要结合业务 很多同学搞反了 其实业务才是最重要的 要避免这种单纯罗列技术栈的格式
简历被挂麻了,求建议
点赞 评论 收藏
分享
牛客57020934...:说实话小厂应该也挺喜欢你这种的,看起来能干活,学历不是那种硕博溢价很高,大厂学历卡死了,但社招和中厂可以多试试。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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