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; } } }
//运行结果截图:
![](https://files.cnblogs.com/files/wjshan0808/0.bmp)