C#中找到最大公约数 [英] C# find the greatest common divisor

查看:461
本文介绍了C#中找到最大公约数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

两个整数的最大公约数是整除每两个数的最大整数。编写方法GCD返回两个整数的最大公约数。纳入该法将一个应用程序从用户读取两个值并显示结果。



(这不是功课,只是在我使用这本书的练习)



你能帮我解决这个问题?下面就是我这么远。谢谢



(编辑 - 我可以提交两个数字,但它不会计算GCD对我来说)

 使用系统; 
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;使用System.Threading.Tasks
;

命名空间Greatest_Common_Divisor
{
类节目
{

静态INT GetNum(字符串文本)
{
布尔IsItANumber = FALSE;
INT X = 0;
Console.WriteLine(文本);


{
IsItANumber = int.TryParse(到Console.ReadLine(),出X);

},而(IsItANumber!);

返回X;
}
静态无效的主要(字串[] args)
{
字符串文本=输入号码;
INT X = GetNum(文本);
文本=输入第二个数字;
INT Y = GetNum(文本);


INT Z = GCD(X,Y);
Console.WriteLine(Z);
}

私有静态诠释GCD(INT X,int y)对
{
INT V = 0;
INT N = 0;

)(V = GetGreatestDivisor(X,Y);


返回伏;

}

静态INT GetGreatestDivisor(INT男,INT高)
{


{
的for(int i =米; I< = 1;我 - )



如果(M%我== 0安培;&安培; H%I == 0)
{
INT X = 0;
X = 1;

返回X;
}
},而(真);
返回米;
}

}
}


解决方案

使用LINQ:

 静态INT GCD(INT []号)
{
返回numbers.Aggregate(GCD);
}



不使用LINQ:

 静态INT GCD(INT A,INT b)
{
回复b == 0?答:GCD(B,A%B);
}

请注意:答案以上从接受的答案借来的Greatest公因式的。


"The greatest common divisor of two integers is the largest integer that evenly divides each of the two numbers. Write method Gcd that returns the greatest common divisor of two integers. Incorporate the method into an app that reads two values from the user and displays the result."

(this is not homework, just an exercise in the book I'm using)

can you help me solve this? Here's what I've got so far. Thanks

(edit - I can submit the two numbers but it won't calculate the Gcd for me)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Greatest_Common_Divisor
{
class Program
{

    static int GetNum(string text)
    {
        bool IsItANumber = false;
        int x = 0;
        Console.WriteLine(text);

        do
        {
            IsItANumber = int.TryParse(Console.ReadLine(), out x);

        } while (!IsItANumber);

        return x;
    }
    static void Main(string[] args)
    {
        string text = "enter a number";
        int x = GetNum(text);
        text = "enter a second number";
        int y = GetNum(text);


        int z = GCD(x, y);
        Console.WriteLine(z);
    }

    private static int GCD(int x, int y)
    {
        int v = 0;
        int n = 0;

        v = GetGreatestDivisor(x, y);


        return v;

    }

    static int GetGreatestDivisor(int m, int h)
        {

            do
            {
                for (int i = m; i <= 1; i--)



                    if (m%i == 0 && h%i == 0)
                    {
                        int x = 0;
                        x = i;

                        return x;
                    }
            } while (true);
            return m;
        }

  }
}

解决方案

Using LINQ:

static int GCD(int[] numbers)
{
    return numbers.Aggregate(GCD);
}

Not using LINQ:

static int GCD(int a, int b)
{
    return b == 0 ? a : GCD(b, a % b);
}

Note: answer above borrowed from accepted answer to Greatest Common Divisor from a set of more than 2 integers.

这篇关于C#中找到最大公约数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆