先来介绍下这个名词的来源:
摘自百度百科:
package com.henu.test; import java.util.HashMap; import java.util.Map; public class MathDemo { public static void main(String[] args) { //在这里输入你要求的两个数 Map<String, Object> map = math(98,63); for(Map.Entry<String, Object> entry:map.entrySet()){ System.out.println(entry.getKey()+"----"+entry.getValue()); } } /** * 更相减损术 * @param a * @param b * @return */ public static Map<String, Object> math(int a, int b){ Map<String, Object> map = new HashMap<>(); int count = 0; int temp = 0; int max =0; for(;a%2==0 && b%2==0;){ a=a/2; b=b/2; count++; } out : while((a-b>0 && a-b !=b) ||(b>a && b-a != a) || a == b || b-a == a || a - b == b){ // while(a-b==0 || b-a==0){ if (a == b ) { temp = b; break out; }else if ( b>a && b-a != a) { temp = b-a; b = temp; }else if (a-b>0 && a-b !=b) { temp = a-b; a = temp; }else if (b-a == a ) { temp = a; break out; }else if (a - b == b ) { temp = b; break out; } } max = (int) Math.pow(2, count) * temp; //计算到最后a的值 map.put("a", a); //计算到最后b的值 map.put("b", b); //表示共同被2整除的次数 map.put("count", count); //最后求得的最大公约数 map.put("max", max); return map; } }
原文地址:http://www.cnblogs.com/xiaoguaishou/p/7190489.html
原文:http://www.cnblogs.com/xiaoguaishou/p/7190489.html