求兩個數的,最大公約數

        /// <summary>
        ///求兩個數的,最大公約數。  如:42 30 的最大公約數爲6
        ///求最大公约数的方法:两个数求最大公约数,可以用辗转相除法。
        ///始终用较大数除以较小数,然后用余数代替较大数。整除时的除数就是最大公约数。  欧几里德算法
        /// </summary>
        /// <param name="param1"></param>
        /// <param name="param2"></param>
        /// <returns></returns>
        static int CommonDivisor(int x, int y)
        {
            if (x > y)
            {
                if (y == 0)
                {
                    return x;
                }

                return CommonDivisor(y, x % y);
            }
            else if (x == y)
            {
                return x;
            }
            else
            {
                return CommonDivisor(x, y % x);
            }
posted on 2009-02-10 17:06  感動常在  阅读(407)  评论(0编辑  收藏  举报