最小公倍数的算法

求最小公倍数:两数的积除两数的最大公约数

  static void Main(string[] args)
            {
            Console.WriteLine("请输入第一个整数:");
            int one = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("请输入第二个整数:");
            int two = Convert.ToInt32(Console.ReadLine());
            int zuiDGYS = MaxGYS(one, two);//调用方法
            int gongBS = one * two / zuiDGYS;
            Console.WriteLine("你输入的两个整数是{0}和{1},他们的最大公约数是{2},最小公倍数是{3}", one, two, zuiDGYS,gongBS );
            }
        static int MaxGYS(int a, int b)
            {
            int tim = Math.Max (a, b);
            b = Math.Min(a, b);
            a = tim;
            while (b != 0)          //b!=0  即被除数不能为零;
                {
                a = a > b ? a : b;   //要使a的值大于b 的值;
                int rem = a % b;    //a,b的余数rem  
                a  = b ;            //这时候,b的值换到a值的位置,rem的值换到b值的位置,
                b  = rem ;          //进行循环,到余数为0,

                } 
            return a;              //注意:返回值是 a,最后的b值是换到了a的位置进行除余为0 的        

            }

在这里插入图片描述

发布了8 篇原创文章 · 获赞 2 · 访问量 68

猜你喜欢

转载自blog.csdn.net/yy52520/article/details/104910656