题解 | #求最小公倍数#

求最小公倍数

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

  1. 最小公倍数 = a * b / 最大公约数
  2. a b的最大公约数

use std::io::{self, *};

fn main() {
    let stdin = io::stdin();
    let input = stdin.lock().lines().next().unwrap().unwrap();
    let numbers:Vec<i32> = input.split(" ").map(|x| x.parse::<i32>().unwrap()).collect();
    let mut a = numbers[0];
    let mut b = numbers[1];

    //a b的最大公约数
    let mut big = if a > b {a} else {b};
    let mut small = if a > b {b} else {a};
    while small!= 0 {
        let temp = big % small;
        big = small;
        small = temp;
    }
    // 最小公倍数 = a * b / 最大公约数
    println!("{}", a * b / big);

}

#rust#
全部评论

相关推荐

腾讯 后台开发 22k+3w
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务