1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace AlgorithmBase6


{7
public class MaxCommonDivisor8

{9
public static int GetMaxCommonDivisor(int m,int n)10

{11
if (m < n)12

{13
int temp = m;14
m = n;15
n = temp;16
}17

18
int r = m % n;19

20
while (r != 0)21

{22
m = n;23
n = r;24
r = m % n;25
}26

27
return n;28
}29
}30
}
本文介绍了一个用于计算两个整数最大公约数(GCD)的算法实现。通过使用辗转相除法,该方法能有效地找到两个数的最大公约数。代码首先确保了较大的数作为除数,随后通过循环迭代直到余数为零,此时的除数即为最大公约数。

被折叠的 条评论
为什么被折叠?



