题解 | #求最小公倍数#

求最小公倍数

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

欧几里得算法
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (scanner.hasNext()) {
            int num1 = scanner.nextInt();
            int num2 = scanner.nextInt();
            int gcd = gcd ( num1, num2 );
            int lcm = num1 * num2 / gcd;
            System.out.println(lcm);
        }
        /*主方法分割线-----------------------------------*/
    }
    public static int gcd(int a, int b) {
        while (b != 0) {
            int tmp = b;
            b = a % tmp;
            a = tmp;
        }
        return a;
    }
}

全部评论

相关推荐

06-19 19:06
门头沟学院 Java
码农索隆:别去东软,真学不到东西,真事
点赞 评论 收藏
分享
06-13 10:15
门头沟学院 Java
想去夏威夷的大西瓜在...:我也是27届,但是我现在研一下了啥项目都没有呀咋办,哎,简历不知道咋写
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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