zoukankan      html  css  js  c++  java
  • C#实现用欧几里德算法、连续整数检测算法、公因数算法求两个非负整数的最大公约数

    源文件:
    http://pan.baidu.com/share/link?shareid=2840221704&uk=3912660076

    //Main:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace GreatestCommonDivisor
    {
        class Program
        {
            static void Main(string[] args)
            {
                Function obj = new Function();
                while (true)
                {
                    Console.WriteLine("Please choose:");
                    Console.WriteLine("1.Euclid’s Algorithm;");
                    Console.WriteLine("2.Consecutive Integer Detection Algorithm;");
                    Console.WriteLine("3.Common Divisor Algorithm;");
                    Console.WriteLine("4.Exit;");
    
                    int number = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine();
    
                    Console.WriteLine("Please enter two number:");
                    Console.Write("one:");
                    int one = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine();
                    Console.Write("another:");
                    int two = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine();
    
                    switch (number)
                    {
                        case 1:
                            Console.Write("Euclid’s Algorithm result:");
                            Console.WriteLine(obj.EuclidAlgorithm(one, two));
                            break;
                        case 2:
                            Console.Write("Consecutive Integer Detection Algorithm result:");
                            Console.WriteLine(obj.ConsecutiveIntegerDetectionAlgorithm(one, two));
                            break;
                        case 3:
                            Console.Write("Common Divisor Algorithm result:");
                            Console.WriteLine(obj.CommonDivisorAlgorithm(one, two));
                            break;
                        case 4:
                            Environment.Exit(0);
                            break;
                    }
                }
            }
        }
    }

    //Class:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace GreatestCommonDivisor
    {
        class Function
        {
            /// <summary>
            /// 欧几里德算法:
            ///     附:(如果两个数都为0.返回0,其中一个为0,返回非0的数)。
            ///     两个都不为0,让大的除以小的,转变为小的变成下一轮的大的,余数变成下一轮的小的。
            ///     以此类推....至余数为0,返回小的数。即结果。 
            /// </summary>
            /// <param name="one"></param>
            /// <param name="two"></param>
            /// <returns></returns>
            public int EuclidAlgorithm(int one, int two)
            {
                int CommonDivisor;
                if (one == 0 && two == 0)
                    return CommonDivisor = 0;
    
                if (one == 0 || two == 0)
                {
                    if (one == 0)
                        CommonDivisor = two;
                    else
                        CommonDivisor = one;
                }
                else
                {
                    int mod = one % two;
                    while (mod != 0)
                    {
                        one = two;
                        two = mod;
                        mod = one % two;
                    }
                    CommonDivisor = two;
                }
                return CommonDivisor;
            }
    
            /// <summary>
            /// 连续整数检测算法:
            ///         附:(如果两个数,其中一个为0,返回非0的数)。
            ///             取两个数中的最小数,如果两个数都除以最小的数余数同时为0,返回最小数,即结果;
            ///      否则在最小的数减1后,重复上述步骤,
            /// </summary>
            /// <param name="one"></param>
            /// <param name="two"></param>
            /// <returns></returns>
            public int ConsecutiveIntegerDetectionAlgorithm(int one, int two)
            {
                int CommonDivisor;
                int minimum;
                if (one == 0 || two == 0)
                {
                    if (one == 0)
                        CommonDivisor = two;
                    else
                        CommonDivisor = one;
                }
                else
                {
                    if (one < two)
                        minimum = one;
                    else
                        minimum = two;
                    while (minimum >= 0)
                    {
                        if (one % minimum == 0 && two % minimum == 0)
                            return minimum;
                        --minimum;
                    }
                    return CommonDivisor = minimum;
                }
                return CommonDivisor;
            }
    
            /// <summary>
            /// 公因数算法:
            ///         找出两个数中的共同拥有的质因数,返回他们的积,即结果.
            ///  附:(如果两个数,其中一个为0,返回非0的数)。
            /// </summary>
            /// <param name="one"></param>
            /// <param name="two"></param>
            /// <returns></returns>
            public int CommonDivisorAlgorithm(int one, int two)
            {
                int CommonDivisor;
                int multiply = 1;
                if (one == 0 || two == 0)
                {
                    if (one == 0)
                        CommonDivisor = two;
                    else
                        CommonDivisor = one;
                }
                else
                {
                    int minimum = one;
    
                    if (one > two)
                        minimum = two;
    
                    if (one != two)
                    {
                        if (two % one == 0 || one % two == 0)
                            CommonDivisor = minimum;
                        else
                        {
                            for (int i = 2; i < minimum; i++)
                            {
                                if (one % i == 0 && two % i == 0)
                                    multiply *= i;
                            }
                            CommonDivisor = multiply;
                        }
                    }
                    else
                        CommonDivisor = one;
                }
                return CommonDivisor;
            }
        }
    }

    //运行结果截图:


  • 相关阅读:
    Symfony2 多bundle及实体获取,app_dev及app下调试
    Symfony2 WebService(一) 配置篇
    Symfony2 数据调用常用方法
    Symfony2 配置自己的log日志、使用配置文件定义全局常量
    Symfony2 环境及基本命令
    Asterisk拨号函数Dial()详解
    C语言系统资源控制(getrlimit && setrlimit)
    centos FTP服务器的架设和配置
    MYSQL远程登录权限设置
    SIP keepalive方法
  • 原文地址:https://www.cnblogs.com/wjshan0808/p/3021303.html
Copyright © 2011-2022 走看看