最大公因数 多个数的最简整数比之和

最大公因数

/**
	 * 俩数 的最大公因数
	 * YYY 2020年12月18日
	 * @param m
	 * @param n
	 * @return
	 */
    public static int getGCD(int m,int n){
    	int result = 0;
        while (n != 0) {
            result = m % n;
            m = n;
            n = result;
        }
        return m;
    }

多个数 最简整数比

算出第一个数 和其他数的 最大公因数

再从这些最大公因数里 找最小的

再用 这堆数 分别 除 这个 最小的最大公因数

例 8 8 12 16

8和8的最大公因数 是 8

8和12的最大公因数 是 4

8和16的最大公因数 是 8

最小的 最大公因数 是 4

分别除

得到 最简整数比 为 2 2 3 4

最简整数比之和 代码如下

/**
	 * 取A : 各组 试验例数 的最简整数比之和
	 * @return
	 */
	public static int getA(int[] numArray) {
		// 算出 第一个和每个数的 最大公因数
		int[] greatestCommonFactorArray = new int[numArray.length - 1];
		for (int i = 1; i < numArray.length; i++) {
			greatestCommonFactorArray[i - 1] = getGCD(numArray[0],numArray[i]);
			System.out.println(greatestCommonFactorArray[i - 1]);
		}

		// 取 最小的 最大公因数
		int greatestCommonFactor = greatestCommonFactorArray[0];
		for (int i = 0; i < greatestCommonFactorArray.length; i++) {
			if (greatestCommonFactor > greatestCommonFactorArray[i]) {
				greatestCommonFactor = greatestCommonFactorArray[i];
			}
		}
		System.out.println(greatestCommonFactor);

		System.out.println(greatestCommonFactor);
		int a = 0;
		// 最简整数比之和
		for (int num : numArray) {
			a += (num / greatestCommonFactor);
			System.out.println(greatestCommonFactor);
		}

		System.out.println("最简整数比之和:"+ a);

		return a;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值