Bob103

C#_4_(V-7)

Oct 10th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1.   class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.            
  6.             Console.Write("Введите размер:");
  7.             int n = Convert.ToInt32(Console.ReadLine());
  8.             int[] arr=new int [n];
  9.             for (int i=0;i<n;i++)
  10.             {
  11.                 Console.Write("Arr[{0}]=", i);
  12.                 arr[i] = Convert.ToInt32(Console.ReadLine());
  13.             }
  14.             Console.WriteLine("Текущий массив: {0}", string.Join(", ", arr));
  15.  
  16.              int k;
  17.             do
  18.             {
  19.                 Console.Write("Введите элемент для вставки: ");
  20.             }
  21.             while (!int.TryParse(Console.ReadLine(), out k));
  22.  
  23.             int last = 0;
  24.             for (int i = arr.Length - 1; i > 0; --i)
  25.                 if (arr[i] > 0)
  26.                 {
  27.                     last = i;
  28.                     break;
  29.                 }
  30.  
  31.             int[] narr = new int[arr.Length + 1];
  32.             for (int i = 0, j = 0; j < narr.Length; ++i, ++j)
  33.             {
  34.                 narr[j] = arr[i];
  35.                 if (j == last)
  36.                 {
  37.                     ++j;
  38.                     narr[j] = k;
  39.                 }
  40.             }
  41.             Console.WriteLine("Измененный массив: {0}", string.Join(", ", narr));
  42.         }
  43.     }
Advertisement
Add Comment
Please, Sign In to add comment