Advertisement
Fanta-MindTerror

Untitled

Dec 19th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.39 KB | None | 0 0
  1. static void Main(string[] args)
  2.         {
  3.             Console.WriteLine("Введите N");
  4.             int N = inputInt();
  5.             int a, b;
  6.             summaRada(N, out a, out b);
  7.             Display(a, b);
  8.             Console.ReadKey();
  9.         }
  10.  
  11.        static void summaRada(int N, out int summA, out int summB)
  12.        {
  13.             summA = -1;
  14.             summB = 1;
  15.  
  16.             if( N <= 2 )
  17.             {
  18.                 summA = 1;
  19.                 summB = 1;
  20.                 return;
  21.             }
  22.  
  23.             for (int i = 2; i < N; i++)  
  24.             {
  25.                 summA *= -1;
  26.                 Summa(ref summA, ref summB, i - 1, i * (i + 1) );
  27.             }
  28.        }
  29.  
  30.  
  31.         static int inputInt()
  32.         {
  33.             int x;
  34.             string inputStr = Console.ReadLine();
  35.             while (!Int32.TryParse(inputStr, out x))
  36.             {
  37.                 Console.WriteLine("Некорректный ввод");
  38.                 inputStr = Console.ReadLine();
  39.             }
  40.             return x;
  41.  
  42.         }
  43.  
  44.         static void Summa(ref int a, ref int b, int c, int d)
  45.         {
  46.             a = a * d + b * c;
  47.             b = b * d;
  48.             Socr(ref a, ref b);
  49.         }
  50.  
  51.         static void Socr(ref int a, ref int b)
  52.         {
  53.             int myNod = nod(a, b);
  54.             if (myNod != 0)
  55.             {
  56.                 a /= myNod;
  57.                 b /= myNod;
  58.             }
  59.         }
  60.  
  61.         static int nod(int a, int b)
  62.         {
  63.             a = Math.Abs(a);
  64.             b = Math.Abs(b);
  65.  
  66.             if (a == 0 && b == 0)
  67.             {
  68.                 return 1;
  69.             }
  70.  
  71.             if (a == 0)
  72.             {
  73.                 return b;
  74.             }
  75.             if (b == 0)
  76.             {
  77.                 return a;
  78.             }
  79.  
  80.             while (a != b)
  81.             {
  82.                 if (a > b)
  83.                     a -= b;
  84.                 else
  85.                 {
  86.                     b -= a;
  87.                 }
  88.             }
  89.             return a;
  90.         }
  91.  
  92.         static int Count(int x)
  93.         {
  94.             int delitel = 0;
  95.             x = Math.Abs(x);
  96.             if (x == 0) x = 1;
  97.             while (x > 0)
  98.             {
  99.                 x /= 10;
  100.                 delitel++;
  101.             }
  102.             return delitel;
  103.  
  104.         }
  105.  
  106.         static void Display(int a, int b)
  107.         {
  108.             Console.ForegroundColor = ConsoleColor.Magenta;
  109.             Console.WriteLine("\n" + a);
  110.             int count = 0;
  111.             int countA = Count(a);
  112.             int countB = Count(b);
  113.  
  114.             Console.ForegroundColor = ConsoleColor.White;
  115.  
  116.             if (a < 0)
  117.             {
  118.                 countB--;
  119.                 Console.Write("-");
  120.             }
  121.             if (b < 0)
  122.             {
  123.                 countA--;
  124.                 Console.Write("-");
  125.             }
  126.  
  127.             if (countA > countB)
  128.             {
  129.                 count = countA;
  130.             }
  131.             else
  132.             {
  133.                 count = countB;
  134.             }
  135.             Console.ForegroundColor = ConsoleColor.White;
  136.             while (count > 0)
  137.             {
  138.                 Console.ForegroundColor = ConsoleColor.White;
  139.                 Console.Write("-");
  140.                 count--;
  141.             }
  142.             Console.ForegroundColor = ConsoleColor.Magenta;
  143.             Console.WriteLine("\n" + b);
  144.  
  145.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement