Advertisement
ralichka

Exam-06.August.2019-02.TreasureHunt

Oct 29th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _02.TreasureHunt
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<string> treasureChest = Console.ReadLine().Split('|').ToList();
  12.  
  13.             string[] command = Console.ReadLine().Split();
  14.             string word = command[0];
  15.  
  16.             while (word != "Yohoho!")
  17.             {
  18.                 if (word == "Loot")
  19.                 {
  20.  
  21.                     for (int i = 1; i < command.Length; i++)
  22.                     {
  23.                         string current = command[i];
  24.  
  25.                         if (!(treasureChest.Contains(current)))
  26.                         {
  27.                             treasureChest.Insert(0, current);
  28.                         }
  29.                     }
  30.                 }
  31.  
  32.  
  33.                 if (word == "Drop")
  34.                 {
  35.                     int index = int.Parse(command[1]);
  36.  
  37.                     if (index >= 0 && index < treasureChest.Count)
  38.                     {
  39.                         string droppedElement = treasureChest[index];
  40.                         treasureChest.RemoveAt(index);
  41.                         treasureChest.Add(droppedElement);
  42.                     }
  43.                     else
  44.                     {
  45.                         command = Console.ReadLine().Split();
  46.                         word = command[0];
  47.                         continue;
  48.                     }
  49.                 }
  50.  
  51.  
  52.                 if (word == "Steal")
  53.                 {
  54.                     int count = int.Parse(command[1]);
  55.                     List<string> stollenElements = new List<string>();
  56.  
  57.  
  58.                     if (count > treasureChest.Count)
  59.                     {
  60.                         count = treasureChest.Count;
  61.                     }
  62.  
  63.                     for (int i = count; i > 0; i--)
  64.                     {
  65.                         string lastElement = treasureChest[treasureChest.Count - 1];
  66.                         treasureChest.Remove(lastElement);
  67.                         stollenElements.Add(lastElement);
  68.                     }
  69.                     stollenElements.Reverse();
  70.                     Console.WriteLine(string.Join(", ", stollenElements));
  71.  
  72.  
  73.  
  74.                 }
  75.  
  76.                 command = Console.ReadLine().Split();
  77.                 word = command[0];
  78.  
  79.             }
  80.  
  81.             double sum = 0;
  82.             double average = 0;
  83.  
  84.             for (int i = 0; i < treasureChest.Count; i++)
  85.             {
  86.                 sum += treasureChest[i].Length;
  87.             }
  88.             average = sum / treasureChest.Count;
  89.  
  90.             if (treasureChest.Count > 0)
  91.             {
  92.                 Console.WriteLine($"Average treasure gain: {average:f2} pirate credits.");
  93.             }
  94.             else
  95.             {
  96.                 Console.WriteLine("Failed treasure hunt.");
  97.             }
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement