【练习16】求最小公倍数

链接:求最小公倍数_牛客题霸_牛客网 (nowcoder.com)

题目分析:

要求最小公倍数,要先用辗转相除法求最大公约数。假如有两个数a、b:

最小公倍数=a*b / a和b的最大公约数

最大公约数 = (b, a % b),直到b为0

代码分析:

java 复制代码
import java.util.Scanner;

public class Main {
  public static int gcd(int a, int b){
    if(b == 0) return a;
    return gcd(b, a % b);
  }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a = in.nextInt(), b = in.nextInt();
        System.out.println(a * b / gcd(a,b));
    }
}
相关推荐
tqs_123451 天前
倒排索引数据结构
java·前端·算法
2501_944526421 天前
Flutter for OpenHarmony 万能游戏库App实战 - 抽牌游戏实现
android·开发语言·python·flutter·游戏
a程序小傲1 天前
听说前端又死了?
开发语言·前端·mysql·算法·postgresql·深度优先
饱饱要坚持可持续发展观1 天前
SpringBoot 集成 Liquibase
java·spring boot·后端
学Linux的语莫1 天前
python项目打包为镜像
java·python·spring
Ashley_Amanda1 天前
Python 进阶:从熟练到精通的核心技能体系
开发语言·python
你怎么知道我是队长1 天前
C语言---命令行参数
c语言·开发语言
秋刀鱼程序编程1 天前
Java编程基础入门(四)---选择循环语句
java·开发语言·算法
一条咸鱼_SaltyFish1 天前
WebFlux vs MVC:Gateway集成若依框架的技术选型之争
java·开发语言·微服务·gateway·mvc·开源软件·webflux
独自归家的兔1 天前
Java反射之根:Class类生成机制深度剖析与最佳实践
java·开发语言