Advertisement
silvana1303

treasure hunt

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