一行代码求两个数的最大公约数(Java)
分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请轻击http://www.captainbed.net
package live.every.day.Programming;
/**
* 题目:
* 一行代码求两个数的最大公约数。
*
* 思路:
* 辗转相除法。
*
* @author Created by LiveEveryDay
*/
public class OneLineCodeGCD {
public static int gcd(int m, int n) {
return n == 0 ? m : gcd(n, m % n);
}
public static void main(String[] args) {
int m = 32875;
int n = 325;
System.out.printf("The result is: %d", gcd(m, n));
}
}
// ------ Output ------
/*
The result is: 25
*/