Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp2
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int[] arr = { 1, 2, 3, 5, 6 };
  14.             //Console.WriteLine("Введите массив: ");
  15.             Console.WriteLine("Введите элемент: ");
  16.             int elem = int.Parse(Console.ReadLine());
  17.             for (int i = 0; i < arr.Length; ++i)
  18.             {
  19.                 if (elem < arr[i])
  20.                 {
  21.                     var tmp2 = arr.ToList();
  22.                     tmp2.Insert(i, elem);
  23.                     arr = tmp2.ToArray();
  24.                     break;
  25.                 }
  26.             }
  27.  
  28.             int index = 0;
  29.             int summ = -1;
  30.             for (int i = 0; i < arr.Length; ++i)
  31.             {
  32.                 int temp = 0;
  33.                 var str = arr[i].ToString().ToCharArray();
  34.                 for (int j = 0; j < str.Length; ++j)
  35.                     temp += (int)(str[j] - '0');
  36.                 if (temp < summ || summ == -1)
  37.                 {
  38.                     summ = temp;
  39.                     index = i;
  40.                 }
  41.             }
  42.             var tmp = arr.ToList();
  43.             tmp.RemoveAt(index);
  44.             arr = tmp.ToArray();
  45.  
  46.             foreach (int i in arr)
  47.                 Console.Write(i + " ");
  48.             Console.WriteLine();
  49.             Console.ReadKey();
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement