【问题标题】:Least Common Multiple最小公倍数
【发布时间】:2020-11-13 19:07:33
【问题描述】:

我有当前的编码,它曾经是一个 goto,但我被告知不要再使用 goto,因为它不受欢迎。我在将其更改为 for 循环时遇到了麻烦。我对 C# 和一般编程相当陌生,所以其中一些对我来说是全新的东西。任何帮助,将不胜感激。实际问题是输入两个数,求最小公倍数。

这是带有 goto 的原件:

BOB:
    if (b < d)
    {                
        a++;
        myInt = myInt * a;
        b = myInt;
        myInt = myInt / a;

        if (b % myInt2 == 0)
        {
            Console.Write("{0} ", h);
            Console.ReadLine();
        }

    }
    if (d < b)
    {
        c++;
        myInt2 = myInt2 * c;
        d = myInt2;
        myInt2 = myInt2 / c;

        if (d % myInt == 0)
        {
            Console.Write("{0} ", t);
            Console.ReadLine();
        }
        else
        {
            goto BOB;
        }

    }
    else
    {
        goto BOB;
    }

   }

【问题讨论】:

  • 如果d == b 怎么办?如果您向我们展示包含goto 的原始代码,也会容易得多。否则,“c#最小公倍数”的快速谷歌可能会显示一些有用的结果......
  • 更重要的是......问题是什么?
  • 如果这是家庭作业,你可能想和你的助教谈谈。
  • @plinth 这是一个独立的学习班,没有人问我该做什么。

标签: c# lcm


【解决方案1】:

这是最小公倍数计算的更有效和简洁的实现,它利用了它与最大公因数(又名最大公约数)的关系。这个最大公因数函数使用欧几里得算法,比 user1211929 或 Tilak 提供的解决方案更有效。

static int gcf(int a, int b)
{
    while (b != 0)
    {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

static int lcm(int a, int b)
{
    return (a / gcf(a, b)) * b;
}

有关更多信息,请参阅有关计算 LCM 和 GCF 的维基百科文章。

【讨论】:

【解决方案2】:

试试这个:

using System;

public class FindLCM
{
    public static int determineLCM(int a, int b)
    {
        int num1, num2;
        if (a > b)
        {
            num1 = a; num2 = b;
        }
        else
        {
            num1 = b; num2 = a;
        }

        for (int i = 1; i < num2; i++)
        {
            int mult = num1 * i;
            if (mult % num2 == 0)
            {
                return mult;
            }
        }
        return num1 * num2;
    }

    public static void Main(String[] args)
    {
        int n1, n2;

        Console.WriteLine("Enter 2 numbers to find LCM");

        n1 = int.Parse(Console.ReadLine());
        n2 = int.Parse(Console.ReadLine());

        int result = determineLCM(n1, n2);

        Console.WriteLine("LCM of {0} and {1} is {2}",n1,n2,result);
        Console.Read();
    }
}

输出:

Enter 2 numbers to find LCM
8
12
LCM of 8 and 12 is 24

【讨论】:

  • 谢谢,但我想了解我在做什么,而不仅仅是从别人那里获取。
【解决方案3】:

试试这个

 int number1 = 20;
 int number2 = 30;
 for (tempNum = 1; ; tempNum++)
 {
   if (tempNum % number1 == 0 && tempNum % number2 == 0)
   {
       Console.WriteLine("L.C.M is - ");
       Console.WriteLine(tempNum.ToString());
       Console.Read();
       break;
    }
 }

// output -> L.C.M is - 60

【讨论】:

    【解决方案4】:
            int num1, num2, mull = 1;
    
            num1 = int.Parse(Console.ReadLine());  
            num2 = int.Parse(Console.ReadLine());
    
            for (int i = 1; i <= num1; i++)
            {
                for (int j = 1; j <= num2; j++)
                {
                    if (num1 * j == num2 * i)
                    {
                        mull = num2 * i;
                        Console.Write(mull); 
                        return;
                    }
                }
            }
    

    【讨论】:

      【解决方案5】:

      这里是查找 LCM 的优化解决方案。

       private static int lcmOfNumbers(int num1, int num2)
          {
              int temp = num1 > num2 ? num1 : num2;
              int counter = 1;
              while (!((temp* counter++) % num1 == 0 && (temp* counter++) % num2 == 0)) {
              }
              return temp* (counter-2);
          }
      

      【讨论】:

      • 你好。我的印象是,“优化很多”是指 while hack,但你能解释一下如何以及为什么这实际上比更容易阅读的代码更快吗?我的普遍印象是编译器在优化代码方面通常比人类好得多。
      【解决方案6】:

      这是一种递归解决方案。这可能是关于一些面试问题。希望对你有帮助

          public static int GetLowestDenominator(int a, int b, int c = 2)
          { 
              if (a == 1 | b == 1) {
                  return 1;
              }
              else if (a % c == 0 & b % c == 0)
              {
                  return c;
              }
              else if (c < a & c < b)
              {
                  c += 1;
                  return GetLowestDenominator(a, b, c);
              }
              else
              {
                  return 0;
              }
          }
      

      【讨论】:

        【解决方案7】:
        int n1 = 13;
        int n2 = 26;
        
        for (int i = 2; i <= n1; i++)
         {
            if (n1 % i == 0 && n2 % i == 0)
            {
            Console.WriteLine("{0} is the LCM of {1} and 
            {2}",i,n1,n2);
            break;
         }
        
          }
        

        【讨论】:

        • 欢迎来到 Stack Overflow!虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。
        猜你喜欢
        • 2020-07-11
        • 2021-07-16
        • 2020-10-12
        • 2016-05-12
        • 1970-01-01
        • 1970-01-01
        • 2016-11-19
        • 2020-09-05
        相关资源
        最近更新 更多