Advertisement
Guest User

5_II_9_ы

a guest
Sep 23rd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.90 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ForDZ
  8. {
  9.     class Input
  10.     {
  11.         private IEnumerator<string> istream;
  12.  
  13.         private static IEnumerable<string> GetNullStream()
  14.         {
  15.             while (true) yield return null;
  16.         }
  17.  
  18.         private static IEnumerator<string> GetIStream()
  19.         {
  20.             while (true)
  21.             {
  22.                 IEnumerable<string> sstream;
  23.                 try
  24.                 {
  25.                     sstream = Console.ReadLine().Split().Where(s => s.Length > 0);
  26.                 }
  27.                 catch (Exception)
  28.                 {
  29.                     sstream = GetNullStream();
  30.                 }
  31.  
  32.                 foreach (string s in sstream)
  33.                     yield return s;
  34.             }
  35.         }
  36.  
  37.         public Input()
  38.         {
  39.             istream = GetIStream();
  40.         }
  41.  
  42.         public string GetString()
  43.         {
  44.             istream.MoveNext();
  45.             return istream.Current;
  46.         }
  47.  
  48.         public int GetInt()
  49.         {
  50.             return int.Parse(GetString());
  51.         }
  52.     }
  53.  
  54.     class Program
  55.     {
  56.         static Input reader;
  57.  
  58.         static void Pause()
  59.         {
  60.             Console.WriteLine("Press any key.");
  61.             Console.ReadKey();
  62.         }
  63.  
  64.         static int nod(int a, int b)
  65.         {
  66.             while (a > 0 && b > 0)
  67.             {
  68.                 if (a > b)
  69.                 {
  70.                     a %= b;
  71.                 }
  72.                 else
  73.                 {
  74.                     b %= a;
  75.                 }
  76.             }
  77.  
  78.             return a + b;
  79.         }
  80.  
  81.         static void task_1()
  82.         {
  83.             Console.Write("> ");
  84.             int a = reader.GetInt();
  85.             int b = reader.GetInt();
  86.  
  87.             do
  88.             {
  89.                 int del = nod(a, b);
  90.  
  91.                 if (del == 1) break;
  92.  
  93.                 a /= del;
  94.                 b /= del;
  95.             }
  96.             while (a != 1 && b != 1);
  97.  
  98.             Console.WriteLine($"> {a} / {b}");
  99.         }
  100.  
  101.         static void task_2()
  102.         {
  103.             Console.Write("> ");
  104.             int a = reader.GetInt();
  105.             int b = reader.GetInt();
  106.  
  107.             int res = 0;
  108.             for (int i = ((a > b) ? a : b), end = a * b + 1; i < end; i++)
  109.             {
  110.                 if (i % a == 0 && i % b == 0)
  111.                 {
  112.                     res = i;
  113.                     break;
  114.                 }
  115.             }
  116.  
  117.             Console.WriteLine($"> {res}");
  118.         }
  119.  
  120.         static void task_3()
  121.         {
  122.             Console.Write("> ");
  123.             int a = reader.GetInt();
  124.             int b = reader.GetInt();
  125.  
  126.             int c = reader.GetInt();
  127.             int d = reader.GetInt();
  128.  
  129.             int x = 0, y = 0;
  130.  
  131.             if (b != d)
  132.             {
  133.                 Console.Write($"> ({a} / {b}) == ");
  134.  
  135.                 do
  136.                 {
  137.                     int del = nod(a, b);
  138.  
  139.                     if (del == 1) break;
  140.  
  141.                     a /= del;
  142.                     b /= del;
  143.                 }
  144.                 while (a != 1 && b != 1);
  145.  
  146.                 Console.WriteLine($"({a} / {b})");
  147.  
  148.                 Console.Write($"> ({c} / {d}) == ");
  149.  
  150.                 do
  151.                 {
  152.                     int del = nod(c, d);
  153.  
  154.                     if (del == 1) break;
  155.  
  156.                     c /= del;
  157.                     d /= del;
  158.                 }
  159.                 while (c != 1 && d != 1);
  160.  
  161.                 Console.WriteLine($"({c} / {d})");
  162.  
  163.                 Console.WriteLine($"> ({a} / {b}) + ({c} / {d})");
  164.  
  165.                 if (b != d)
  166.                 {
  167.                     Console.WriteLine($"> ({a} * {d} + {c} * {b}) / ({b} * {d})");
  168.                     x = a * d + c * b;
  169.                     y = d * b;
  170.                 }
  171.                 else
  172.                 {
  173.                     Console.WriteLine($"> ({a} + {c}) / {b}");
  174.                     x = a + c;
  175.                     y = b;
  176.                 }
  177.             }
  178.             else
  179.             {
  180.                 Console.WriteLine($"> ({a} / {b}) + ({c} / {d})");
  181.                 Console.WriteLine($"> ({a} + {c}) / {b}");
  182.                 x = a + c;
  183.                 y = b;
  184.             }
  185.  
  186.             Console.WriteLine($"> ({x} / {y})");
  187.  
  188.             do
  189.             {
  190.                 int del = nod(x, y);
  191.  
  192.                 if (del == 1) break;
  193.  
  194.                 x /= del;
  195.                 y /= del;
  196.             }
  197.             while (x != 1 && y != 1);
  198.  
  199.             Console.WriteLine($"> {x} / {y}");
  200.         }
  201.  
  202.         static void task_4()
  203.         {
  204.             Console.Write("> ");
  205.  
  206.             //int a = reader.GetInt();
  207.             //int b = reader.GetInt();
  208.  
  209.             int n = reader.GetInt();
  210.             int[] a = new int[n];
  211.  
  212.             for (int i = 0; i < n; i++)
  213.             {
  214.                 a[i] = reader.GetInt();
  215.             }
  216.  
  217.             bool have_not_zero = true;
  218.  
  219.             for (int i = 0; i < n; i++)
  220.             {
  221.                 if(a[i] == 0)
  222.                 {
  223.                     have_not_zero = false;
  224.                     break;
  225.                 }
  226.             }
  227.  
  228.             if(!have_not_zero)
  229.             {
  230.                 Console.WriteLine("1");
  231.                 return;
  232.             }
  233.  
  234.  
  235.             while (have_not_zero)
  236.             {
  237.                 int i_min = 0;
  238.                 int min = a[i_min];
  239.  
  240.                 for (int i = 1; i < n; i++)
  241.                 {
  242.                     if(a[i] < min)
  243.                     {
  244.                         i_min = i;
  245.                         min = a[i];
  246.                     }
  247.                 }
  248.  
  249.                 for (int i = 0; i < n; i++)
  250.                 {
  251.                     if (i == i_min) continue;
  252.  
  253.                     a[i] %= min;
  254.                 }
  255.  
  256.                 for (int i = 0; i < n; i++)
  257.                 {
  258.                     if (a[i] == 0)
  259.                     {
  260.                         have_not_zero = false;
  261.                         break;
  262.                     }
  263.                 }
  264.             }
  265.  
  266.             int res = a[0];
  267.  
  268.             for (int i = 1; i < n; i++)
  269.             {
  270.                 if (a[i] != 0  && (res == 0 || a[i] < res))
  271.                 {
  272.                     res = a[i];
  273.                 }
  274.             }
  275.  
  276.             //for (int i = 0; i < n; i++) Console.Write($"{a[i]} "); Console.WriteLine();
  277.             Console.WriteLine($"> {res}");
  278.  
  279.             //int res = nod(a, b);
  280.  
  281.             //Console.WriteLine($"> {res}");
  282.         }
  283.  
  284.         static void Main(string[] args)
  285.         {
  286.             ConsoleKeyInfo key;
  287.             reader = new Input();
  288.  
  289.             for (; ; )
  290.             {
  291.                 Console.Clear();
  292.                 Console.WriteLine("Select:");
  293.                 Console.WriteLine(" 1) fraction");
  294.                 Console.WriteLine(" 2) NOK");
  295.                 Console.WriteLine(" 3) fraction sum");
  296.                 Console.WriteLine(" 4) NOD");
  297.                 Console.WriteLine(" 0) exit");
  298.  
  299.                 key = Console.ReadKey();
  300.                 Console.Clear();
  301.  
  302.                 switch (key.Key)
  303.                 {
  304.  
  305.                     case ConsoleKey.D1:
  306.                         task_1();
  307.                         Pause();
  308.                         continue;
  309.                     case ConsoleKey.D2:
  310.                         task_2();
  311.                         Pause();
  312.                         continue;
  313.                     case ConsoleKey.D3:
  314.                         task_3();
  315.                         Pause();
  316.                         continue;
  317.                     case ConsoleKey.D4:
  318.                         task_4();
  319.                         Pause();
  320.                         continue;
  321.                 }
  322.  
  323.                 if (key.Key == ConsoleKey.D0) break;
  324.             }
  325.  
  326.             //Console.ReadKey();
  327.         }
  328.     }
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement