Игорь Пашев -

Dec. 6th, 2010

04:19 pm

Previous Entry Add to Memories Tell A Friend Next Entry

Каюсь, грешен.
Наибольший общий делитель нескольких чисел на C#.

Использование:
# gmcs gcd.cs
# mono ./gcd.exe 121 11 33 44
11
# mono ./gcd.exe 2 4 6 8
2


 1 using System;
 2
 3 namespace GCD {
 4     class Program {
 5         static uint gcd2(uint a, uint b) {
 6             uint c;
 7             while (b != 0) {
 8                 c = b;
 9                 b = a % b;
10                 a = c;
11             }
12             return a;
13         }
14
15         static uint gcdn(uint [] n) {
16             uint r = n[0];
17             for (int i = 1; i < n.Length; i++)
18                 r = gcd2(r, n[i]);
19             return r;
20         }
21
22         static void Main(string [] argv) {
23             uint [] a = Array.ConvertAll<string, uint>(argv,
24                     new Converter<string, uint>
25                     (delegate(string s) {return uint.Parse(s);})
26                     );
27
28             Console.WriteLine("{0}", gcdn(a));
29         }
30     }
31 }
32

Tags: , ,
(Оставить комментарий)