Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main(string[] args)
- {
- Console.WriteLine("Введите N");
- int N = inputInt();
- int a, b;
- summaRada(N, out a, out b);
- Display(a, b);
- Console.ReadKey();
- }
- static void summaRada(int N, out int summA, out int summB)
- {
- summA = -1;
- summB = 1;
- if( N <= 2 )
- {
- summA = 1;
- summB = 1;
- return;
- }
- for (int i = 2; i < N; i++)
- {
- summA *= -1;
- Summa(ref summA, ref summB, i - 1, i * (i + 1) );
- }
- }
- static int inputInt()
- {
- int x;
- string inputStr = Console.ReadLine();
- while (!Int32.TryParse(inputStr, out x))
- {
- Console.WriteLine("Некорректный ввод");
- inputStr = Console.ReadLine();
- }
- return x;
- }
- static void Summa(ref int a, ref int b, int c, int d)
- {
- a = a * d + b * c;
- b = b * d;
- Socr(ref a, ref b);
- }
- static void Socr(ref int a, ref int b)
- {
- int myNod = nod(a, b);
- if (myNod != 0)
- {
- a /= myNod;
- b /= myNod;
- }
- }
- static int nod(int a, int b)
- {
- a = Math.Abs(a);
- b = Math.Abs(b);
- if (a == 0 && b == 0)
- {
- return 1;
- }
- if (a == 0)
- {
- return b;
- }
- if (b == 0)
- {
- return a;
- }
- while (a != b)
- {
- if (a > b)
- a -= b;
- else
- {
- b -= a;
- }
- }
- return a;
- }
- static int Count(int x)
- {
- int delitel = 0;
- x = Math.Abs(x);
- if (x == 0) x = 1;
- while (x > 0)
- {
- x /= 10;
- delitel++;
- }
- return delitel;
- }
- static void Display(int a, int b)
- {
- Console.ForegroundColor = ConsoleColor.Magenta;
- Console.WriteLine("\n" + a);
- int count = 0;
- int countA = Count(a);
- int countB = Count(b);
- Console.ForegroundColor = ConsoleColor.White;
- if (a < 0)
- {
- countB--;
- Console.Write("-");
- }
- if (b < 0)
- {
- countA--;
- Console.Write("-");
- }
- if (countA > countB)
- {
- count = countA;
- }
- else
- {
- count = countB;
- }
- Console.ForegroundColor = ConsoleColor.White;
- while (count > 0)
- {
- Console.ForegroundColor = ConsoleColor.White;
- Console.Write("-");
- count--;
- }
- Console.ForegroundColor = ConsoleColor.Magenta;
- Console.WriteLine("\n" + b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement