算法--求两个数的最大公约数

版权声明: https://blog.csdn.net/xichengqc/article/details/79946912
package com.xicheng.demo01;

/**
 * 2018年4月15日上午9:56:52
 * Compute the greatest common divisor of
 * two nonnegative integers p and q as follows:
 * If q is 0, the answer is p. If not, divide p by q
 * and take the remainder r. The answer is the
 * greatest common divisor of q and r.
 * 
 */
public class Test01 {
	public static void main(String[] args) {
		int gcd = getGcd(12, 24);
		System.out.println(gcd);
	}
	
	private static int getGcd(int p, int q) {
		if (q == 0) {
			return p;
		}
		int r = p % q;
		return getGcd(q, r);
	}
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页