Advertisement
BeloMaximka

Belov_LW_CS2

Feb 2nd, 2022
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.30 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace Example
  5. {
  6.     class MyApplication
  7.     {
  8.         static Random rand = new Random();
  9.         private static void First()
  10.         {
  11.             int[] array = new int[10];
  12.             for (int i = 0; i < array.Length; i++)
  13.             {
  14.                 array[i] = rand.Next(0, 10);
  15.                 Console.Write("{0} ", array[i]);
  16.             }
  17.             Console.WriteLine();
  18.             for (int i = 0; i < array.Length; i++)
  19.             {
  20.                 if (array[i] == 0)
  21.                     array[i] = -1;
  22.             }
  23.             array = array.OrderBy(n => n == -1).ToArray();
  24.             foreach (int item in array)
  25.                 Console.Write("{0} ", item);
  26.             Console.ReadKey();
  27.             Console.Clear();
  28.         }
  29.         private static void Second()
  30.         {
  31.             int[] array = new int[20];
  32.             for (int i = 0; i < array.Length; i++)
  33.             {
  34.                 array[i] = rand.Next(-10, 30);
  35.                 Console.Write("{0} ", array[i]);
  36.             }
  37.             Console.WriteLine();
  38.             int sum = 0;
  39.             int begin = Array.FindIndex(array, n => n < 0);
  40.             if (begin != -1 && begin != array.Length - 1)
  41.             {
  42.                 begin++;
  43.                 for (int i = begin; i < array.Length; i++)
  44.                     sum += array[i];
  45.             }
  46.             Console.WriteLine(sum.ToString());
  47.             Console.ReadKey();
  48.             Console.Clear();
  49.         }
  50.         private static void Third()
  51.         {
  52.             int[] array = new int[20];
  53.             for (int i = 0; i < array.Length; i++)
  54.             {
  55.                 array[i] = rand.Next(-30, 10);
  56.                 Console.Write("{0} ", array[i]);
  57.             }
  58.             Console.WriteLine();
  59.             int sum = 0;
  60.             int begin = Array.FindIndex(array, n => n > 0);
  61.             if (begin != -1 && begin != array.Length - 1)
  62.             {
  63.                 begin++;
  64.                 for (int i = begin; i < array.Length; i++)
  65.                     sum += array[i];
  66.             }
  67.             Console.WriteLine(sum.ToString());
  68.             Console.ReadKey();
  69.             Console.Clear();
  70.         }
  71.         private static void Fourth()
  72.         {
  73.             int[] array = new int[20];
  74.             for (int i = 0; i < array.Length; i++)
  75.             {
  76.                 array[i] = rand.Next(-10, 10);
  77.                 Console.Write("{0} ", array[i]);
  78.             }
  79.             Console.WriteLine();
  80.             int min = array[0];
  81.             int minIndex = 0;
  82.             int max = array[0];
  83.             int maxIndex = 0;
  84.             for (int i = 0; i < array.Length; i++)
  85.             {
  86.                 if (array[i] < min)
  87.                 {
  88.                     min = array[i];
  89.                     minIndex = i;
  90.                 }
  91.                 else if (array[i] > max)
  92.                 {
  93.                     max = array[i];
  94.                     maxIndex = i;
  95.                 }
  96.             }
  97.  
  98.             Console.WriteLine("{0}, {1}, {2}, {3}", min, minIndex, max, maxIndex);
  99.             Console.ReadKey();
  100.             Console.Clear();
  101.         }
  102.         public static void Main()
  103.         {
  104.             First();
  105.             Second();
  106.             Third();
  107.             Fourth();
  108.         }
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement