编程之美-2.7最大公约数问题java解法

编程之美-最大公约数问题java解法

包括五种方法,最后有一个判断奇偶数的方法isEven();

package code.beauty.fungame;

public class GCD {

	public static void main(String[] args) {
		int gcd = solutionGcd5(30,42);
		System.out.println(gcd);
	}

	private static int solutionGcd(int a, int b) {
		int c = 0;
		boolean flag = true;
		if (a < b) {
			c = a;
			a = b;
			b = c;
		}
		while (flag) {
			c = a % b;
			if (c != 0) {
				a = b;
				b = c;
			} else {
				flag = false;
			}
		}
		return b;

	}
	private static int solutionGcd2(int a, int b){
		return b==0?a:solutionGcd2(b,a % b);
	}
	private static int solutionGcd3(int a, int b){
		int c1 =0, c2=0;
		boolean flag = true;
		int k = 1;
		while(flag){
			c1=a-k*b;
			if(c1>0){
				k= k +1; c2=c1;
			}else{
				flag=false;
			}
		}
		return c2==0?a:solutionGcd3(b,c2);
	}
	private static int solutionGcd4(int a, int b){
		if(a<b){
			return solutionGcd4(b,a);
		}
		if(b==0) return a;
		return solutionGcd4(a-b,b);
	}
	private static int solutionGcd5(int a, int b){
		if(a<b){
			return solutionGcd5(b,a);
		}
		if(b==0) return a;
		
		if(isEven(a)){
			if(isEven(b)){
				return solutionGcd5(a>>1,b>>1)<<1;
			}else{
				return solutionGcd5(a>>1,b);
			}
		}else{
			if(isEven(b)){
				return solutionGcd5(a,b>>1);
			}else{
				return solutionGcd5(a-b,b);
			}
		}
	}
	private static boolean isEven(int i){
		return (i&1)!=1;
	}
	
}

    原文作者:asheng010
    原文地址: https://blog.csdn.net/asheng010/article/details/8687977
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞