[Java] 蓝桥杯ADV-92 算法提高 求最大公约数

版权声明:【https://github.com/liuchuo】大四在校生,水平有限,还望学长们多多包涵,Github真诚求Star~不甚感激!!!(卖萌脸ヾ(=^▽^=)ノ https://blog.csdn.net/liuchuo/article/details/82919311

编写一函数gcd,求两个正整数的最大公约数。
样例输入:
5 15
样例输出:
5
样例输入:
7 2
样例输出:
1

package adv92;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println(gcd(in.nextInt(), in.nextInt()));
        in.close();
    }

    private static int gcd(int a, int b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }
}

猜你喜欢

转载自blog.csdn.net/liuchuo/article/details/82919311