Advertisement
silvana1303

treasure hunt version 2

Jul 2nd, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Numerics;
  5. using System.Threading;
  6. using System.Transactions;
  7.  
  8. namespace exampreparation
  9. {
  10.     class exam
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             List<string> treasure = Console.ReadLine().Split('|').ToList();
  15.  
  16.             string[] command = Console.ReadLine().Split().ToArray();
  17.  
  18.             while (command[0] != "Yohoho!")
  19.             {
  20.                 if (command[0] == "Loot")
  21.                 {
  22.                     List<string> loot = new List<string>();
  23.  
  24.                     for (int i = 1; i < command.Length; i++)
  25.                     {
  26.                         if (!treasure.Contains(command[i]))
  27.                         {
  28.                             loot.Add(command[i]);
  29.                         }
  30.                     }
  31.  
  32.                     loot.Reverse();
  33.                     treasure.InsertRange(0, loot);
  34.                 }
  35.                 if (command[0] == "Drop")
  36.                 {
  37.                     int index = int.Parse(command[1]);
  38.                     if (index >= 0 && index < treasure.Count)
  39.                     {
  40.                         string temp = treasure[index];
  41.                         treasure.RemoveAt(index);
  42.                         treasure.Add(temp);
  43.                     }
  44.                 }
  45.                 if (command[0] == "Steal")
  46.                 {
  47.                     int count = int.Parse(command[1]);
  48.  
  49.                     if (count > treasure.Count)
  50.                     {
  51.                         count = treasure.Count;
  52.                     }
  53.  
  54.                     treasure.Reverse();
  55.                     List<string> steal = treasure.GetRange(0, count);
  56.                     treasure.RemoveRange(0, count);
  57.                     steal.Reverse();
  58.                     Console.WriteLine(string.Join(", ", steal));
  59.                     treasure.Reverse();
  60.                 }
  61.  
  62.                 command = Console.ReadLine().Split().ToArray();
  63.  
  64.             }
  65.  
  66.             double sum = 0;
  67.             for (int i = 0; i < treasure.Count; i++)
  68.             {
  69.                 sum += treasure[i].Length;
  70.             }
  71.             double average = sum / treasure.Count * 1.0;
  72.  
  73.             if (treasure.Count == 0)
  74.             {
  75.                 Console.WriteLine("Failed treasure hunt.");
  76.             }
  77.             else
  78.             {
  79.                 Console.WriteLine($"Average treasure gain: {average:F2} pirate credits.");
  80.             }
  81.         }
  82.  
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement