simeon3000

Shoot List Elements

Jun 29th, 2017
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.91 KB | None | 0 0
  1.             string input = string.Empty;
  2.  
  3.             List<int> myList = new List<int>();
  4.  
  5.             int lastElement = 0;
  6.  
  7.             do
  8.             {
  9.                 input = Console.ReadLine();
  10.  
  11.                 if (input != "bang" && input != "stop")
  12.                 {
  13.                     myList.Insert(0, int.Parse(input));
  14.                 }
  15.                 else if (input == "bang")
  16.                 {
  17.                     if (myList.Count > 0)
  18.                     {
  19.                         int sum = 0;
  20.                         for (int i = 0; i < myList.Count; i++)
  21.                         {
  22.                             sum += myList[i];
  23.                         }
  24.  
  25.                         double average = sum / (double)myList.Count;
  26.  
  27.                         for (int i = 0; i < myList.Count; i++)
  28.                         {
  29.                             if (myList[i] <= average)
  30.                             {
  31.                                 lastElement = myList[i];
  32.                                 myList.RemoveAt(i);
  33.                                 Console.WriteLine($"shot {lastElement}");
  34.                                 break;
  35.                             }
  36.                         }
  37.  
  38.                         for (int i = 0; i < myList.Count; i++)
  39.                         {
  40.                             myList[i]--;
  41.                         }
  42.                     }
  43.                     else
  44.                     {
  45.                         Console.WriteLine($"nobody left to shoot! last one was {lastElement}");
  46.                         return;
  47.                     }
  48.                 }                
  49.             } while (input != "stop");
  50.  
  51.             if (myList.Count > 0)
  52.             {
  53.                 Console.WriteLine($"survivors: " + string.Join(" ", myList));
  54.             }
  55.             else
  56.             {
  57.                 Console.WriteLine($"you shot them all. last one was {lastElement}");
  58.             }
Advertisement
Add Comment
Please, Sign In to add comment