最大公约数与最小公倍数

2016-01-27· 3355 次浏览
## Java ```java public class Solution { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); StreamTokenizer sc = new StreamTokenizer(reader); sc.nextToken(); long num1 = (long) sc.nval; sc.nextToken(); long num2 = (long) sc.nval; System.out.println(lcm(num1, num2)); } // 最大公约数 public static long gcd(long m, long n) { // 若余数为0,返回最大公约数 if (m % n == 0) return n; return gcd(n, m % n); } // 最小公倍数 public static long lcm(long m, long n) { return m n / gcd(m, n); } } ``` ## Scala ```java import scala.io.StdIn object Solution { def main(args: Array[String]) { var arr = StdIn.readLine().trim().split(" ").map(_.toLong) println(lcm(arr(0), arr(1))) } //最大公约数 def gcd(m: Long, n: Long): Long = { // 若余数为0,返回最大公约数 if (m % n == 0) return n return gcd(n, m % n) } // 最小公倍数 def lcm(m: Long, n: Long): Long = { return m n / gcd(m, n) } } ```