Advertisement
Constantine27

Untitled

Oct 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.67 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace Konsa
  5. {
  6.     class Program
  7.     {
  8.         static Random rnd = new Random();
  9.  
  10.         static void Main(string[] args)
  11.         {
  12.  
  13.             do
  14.             {
  15.                 Console.Write("Enter quantity of numbers: ");
  16.                 int n = Check();
  17.                 Console.Write("Enter k: ");
  18.                 int k = Check2();
  19.                 File.WriteAllText("../../out.txt", "");
  20.  
  21.                 int[] arr = GenerateArray(n, k);
  22.                 Writing(ref arr);
  23.                 ConsoleWriteng(ref arr, k);
  24.  
  25.                 Console.WriteLine("Press 'Escape' button to quit the console or press any key to reenter data");
  26.             } while (Console.ReadKey(true).Key != ConsoleKey.Escape); // Повтор решения
  27.         }
  28.  
  29.  
  30.         static int Check()
  31.         {
  32.             int n;
  33.             while (!int.TryParse(Console.ReadLine(), out n) || n <= 0 || n > 100)
  34.             {
  35.                 Console.WriteLine("Wrong\nPlease enter proper data");
  36.                 Console.WriteLine("Press 'Escape' button to quit the console or press any key to reenter data");
  37.                 if (Console.ReadKey(true).Key == ConsoleKey.Escape) Environment.Exit(0);
  38.             }
  39.  
  40.             return n;
  41.         }
  42.  
  43.  
  44.         static int Check2()
  45.         {
  46.             int k;
  47.             while (!int.TryParse(Console.ReadLine(), out k) || k <= 0 || k > 1000)
  48.             {
  49.                 Console.WriteLine("Wrong\nPlease enter proper data");
  50.                 Console.WriteLine("Press 'Escape' button to quit the console or press any key to reenter data");
  51.                 if (Console.ReadKey(true).Key == ConsoleKey.Escape) Environment.Exit(0);
  52.             }
  53.  
  54.             return k;
  55.         }
  56.  
  57.         static int[] GenerateArray(int n, int k)
  58.         {
  59.             int[] arr = new int[n];
  60.             for (int i = 0; i < n; i++)
  61.             {
  62.                 if (i % k == 0)
  63.                 {
  64.                     arr[i] = rnd.Next(-999, 1001);
  65.                 }
  66.  
  67.                 int r;
  68.  
  69.                 do
  70.                 {
  71.                     r = rnd.Next(-3 * k, 5 * k);
  72.                     if (r % 2 == 0)
  73.                     {
  74.                         arr[i] = r;
  75.                     }
  76.                 } while (r % 2 != 0);
  77.             }
  78.  
  79.             return arr;
  80.         }
  81.  
  82.         static void Writing(ref int[] arr)
  83.         {
  84.             for (int i = 0; i < arr.Length; i++)
  85.             {
  86.                 File.AppendAllText("../../out.txt", arr[i].ToString() + " ");
  87.             }
  88.         }
  89.  
  90.         static void ConsoleWriteng(ref int[] arr, int k)
  91.         {
  92.             int max = 0;
  93.             int count = 0;
  94.             int min = 0;
  95.             int max2 = 0;
  96.  
  97.             Console.Write("Array: ");
  98.  
  99.             for (int i = 0; i < arr.Length; i++)
  100.             {
  101.                 Console.Write(arr[i] + " ");
  102.             }
  103.  
  104.             Console.Write("\nMax numbers aliquot k: ");
  105.             File.AppendAllText("../../out.txt", "\nMax numbers aliquot k: ");
  106.  
  107.             for (int i = 0; i < arr.Length; i++)
  108.             {
  109.                 if (arr[i] % k == 0)
  110.                 {
  111.                     count++;
  112.  
  113.                     if (max < arr[i])
  114.                     {
  115.                         max = arr[i];
  116.                         Console.Write(max + " ");
  117.                         File.AppendAllText("../../out.txt", max.ToString());
  118.                     }
  119.                 }
  120.             }
  121.  
  122.             if (max == 0) Console.Write("There is no such numbers, \nthats why Differece is 0 and Difference #2 is equal to max number of array");
  123.  
  124.             int[] arrk = new int[count];
  125.  
  126.             for (int i = 0; i < count; i++)
  127.             {
  128.                 if (arr[i] % k == 0)
  129.                 {
  130.                     arrk[i] = arr[i];
  131.                 }
  132.             }
  133.  
  134.             for (int i = 0; i < arrk.Length; i++)
  135.             {
  136.                 if (min > arrk[i])
  137.                 {
  138.                     min = arrk[i];
  139.                 }
  140.             }
  141.  
  142.             for (int i = 0; i < arrk.Length; i++)
  143.             {
  144.                 if (max2 < arrk[i])
  145.                 {
  146.                     max2 = arrk[i];
  147.                 }
  148.             }
  149.  
  150.             int difference = max2 - min;
  151.             Console.WriteLine("\nDifference: " + difference);
  152.  
  153.             int max3 = 0;
  154.             for (int i = 0; i < arr.Length; i++)
  155.             {
  156.                 if (max3 < arr[i])
  157.                 {
  158.                     max3 = arr[i];
  159.                 }
  160.             }
  161.  
  162.             int difference2 = max3 - min;
  163.             Console.WriteLine("Difference #2: " + difference2);
  164.         }
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement