超强算法-求任意二个数的最大公约数

本文介绍了一个用于计算两个整数最大公约数(GCD)的算法实现。通过使用辗转相除法,该方法能有效地找到两个数的最大公约数。代码首先确保了较大的数作为除数,随后通过循环迭代直到余数为零,此时的除数即为最大公约数。
ContractedBlock.gifExpandedBlockStart.gifcode
 1None.gifusing System;
 2None.gifusing System.Collections.Generic;
 3None.gifusing System.Text;
 4None.gif
 5None.gifnamespace AlgorithmBase
 6ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 7InBlock.gif    public class MaxCommonDivisor
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 9InBlock.gif        public static int GetMaxCommonDivisor(int m,int n)
10ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
11InBlock.gif            if (m < n)
12ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
13InBlock.gif                int temp = m;
14InBlock.gif                m = n;
15InBlock.gif                n = temp;
16ExpandedSubBlockEnd.gif            }

17InBlock.gif
18InBlock.gif            int r = m % n;
19InBlock.gif
20InBlock.gif            while (r != 0)
21ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
22InBlock.gif                m = n;
23InBlock.gif                n = r;
24InBlock.gif                r = m % n;
25ExpandedSubBlockEnd.gif            }

26InBlock.gif
27InBlock.gif            return n;
28ExpandedSubBlockEnd.gif        }

29ExpandedSubBlockEnd.gif    }

30ExpandedBlockEnd.gif}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值