Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ForDZ
- {
- class Input
- {
- private IEnumerator<string> istream;
- private static IEnumerable<string> GetNullStream()
- {
- while (true) yield return null;
- }
- private static IEnumerator<string> GetIStream()
- {
- while (true)
- {
- IEnumerable<string> sstream;
- try
- {
- sstream = Console.ReadLine().Split().Where(s => s.Length > 0);
- }
- catch (Exception)
- {
- sstream = GetNullStream();
- }
- foreach (string s in sstream)
- yield return s;
- }
- }
- public Input()
- {
- istream = GetIStream();
- }
- public string GetString()
- {
- istream.MoveNext();
- return istream.Current;
- }
- public int GetInt()
- {
- return int.Parse(GetString());
- }
- }
- class Program
- {
- static Input reader;
- static void Pause()
- {
- Console.WriteLine("Press any key.");
- Console.ReadKey();
- }
- static int nod(int a, int b)
- {
- while (a > 0 && b > 0)
- {
- if (a > b)
- {
- a %= b;
- }
- else
- {
- b %= a;
- }
- }
- return a + b;
- }
- static void task_1()
- {
- Console.Write("> ");
- int a = reader.GetInt();
- int b = reader.GetInt();
- do
- {
- int del = nod(a, b);
- if (del == 1) break;
- a /= del;
- b /= del;
- }
- while (a != 1 && b != 1);
- Console.WriteLine($"> {a} / {b}");
- }
- static void task_2()
- {
- Console.Write("> ");
- int a = reader.GetInt();
- int b = reader.GetInt();
- int res = 0;
- for (int i = ((a > b) ? a : b), end = a * b + 1; i < end; i++)
- {
- if (i % a == 0 && i % b == 0)
- {
- res = i;
- break;
- }
- }
- Console.WriteLine($"> {res}");
- }
- static void task_3()
- {
- Console.Write("> ");
- int a = reader.GetInt();
- int b = reader.GetInt();
- int c = reader.GetInt();
- int d = reader.GetInt();
- int x = 0, y = 0;
- if (b != d)
- {
- Console.Write($"> ({a} / {b}) == ");
- do
- {
- int del = nod(a, b);
- if (del == 1) break;
- a /= del;
- b /= del;
- }
- while (a != 1 && b != 1);
- Console.WriteLine($"({a} / {b})");
- Console.Write($"> ({c} / {d}) == ");
- do
- {
- int del = nod(c, d);
- if (del == 1) break;
- c /= del;
- d /= del;
- }
- while (c != 1 && d != 1);
- Console.WriteLine($"({c} / {d})");
- Console.WriteLine($"> ({a} / {b}) + ({c} / {d})");
- if (b != d)
- {
- Console.WriteLine($"> ({a} * {d} + {c} * {b}) / ({b} * {d})");
- x = a * d + c * b;
- y = d * b;
- }
- else
- {
- Console.WriteLine($"> ({a} + {c}) / {b}");
- x = a + c;
- y = b;
- }
- }
- else
- {
- Console.WriteLine($"> ({a} / {b}) + ({c} / {d})");
- Console.WriteLine($"> ({a} + {c}) / {b}");
- x = a + c;
- y = b;
- }
- Console.WriteLine($"> ({x} / {y})");
- do
- {
- int del = nod(x, y);
- if (del == 1) break;
- x /= del;
- y /= del;
- }
- while (x != 1 && y != 1);
- Console.WriteLine($"> {x} / {y}");
- }
- static void task_4()
- {
- Console.Write("> ");
- //int a = reader.GetInt();
- //int b = reader.GetInt();
- int n = reader.GetInt();
- int[] a = new int[n];
- for (int i = 0; i < n; i++)
- {
- a[i] = reader.GetInt();
- }
- bool have_not_zero = true;
- for (int i = 0; i < n; i++)
- {
- if(a[i] == 0)
- {
- have_not_zero = false;
- break;
- }
- }
- if(!have_not_zero)
- {
- Console.WriteLine("1");
- return;
- }
- while (have_not_zero)
- {
- int i_min = 0;
- int min = a[i_min];
- for (int i = 1; i < n; i++)
- {
- if(a[i] < min)
- {
- i_min = i;
- min = a[i];
- }
- }
- for (int i = 0; i < n; i++)
- {
- if (i == i_min) continue;
- a[i] %= min;
- }
- for (int i = 0; i < n; i++)
- {
- if (a[i] == 0)
- {
- have_not_zero = false;
- break;
- }
- }
- }
- int res = a[0];
- for (int i = 1; i < n; i++)
- {
- if (a[i] != 0 && (res == 0 || a[i] < res))
- {
- res = a[i];
- }
- }
- //for (int i = 0; i < n; i++) Console.Write($"{a[i]} "); Console.WriteLine();
- Console.WriteLine($"> {res}");
- //int res = nod(a, b);
- //Console.WriteLine($"> {res}");
- }
- static void Main(string[] args)
- {
- ConsoleKeyInfo key;
- reader = new Input();
- for (; ; )
- {
- Console.Clear();
- Console.WriteLine("Select:");
- Console.WriteLine(" 1) fraction");
- Console.WriteLine(" 2) NOK");
- Console.WriteLine(" 3) fraction sum");
- Console.WriteLine(" 4) NOD");
- Console.WriteLine(" 0) exit");
- key = Console.ReadKey();
- Console.Clear();
- switch (key.Key)
- {
- case ConsoleKey.D1:
- task_1();
- Pause();
- continue;
- case ConsoleKey.D2:
- task_2();
- Pause();
- continue;
- case ConsoleKey.D3:
- task_3();
- Pause();
- continue;
- case ConsoleKey.D4:
- task_4();
- Pause();
- continue;
- }
- if (key.Key == ConsoleKey.D0) break;
- }
- //Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement