spasnikolov131

Untitled

Mar 21st, 2023
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace Treasure_Hunt
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. List<string> treasure = Console.ReadLine()
  12. .Split("|", StringSplitOptions.RemoveEmptyEntries)
  13. .ToList();
  14.  
  15. List<string> steal = new List<string>();
  16.  
  17. while (true)
  18. {
  19. string command = Console.ReadLine();
  20.  
  21. if (command == "Yohoho!")
  22. {
  23. break;
  24. }
  25.  
  26. string[] token = command.Split();
  27. string word = token[0];
  28.  
  29. if (word == "Loot")
  30. {
  31. for (int i = 1; i < token.Length; i++)
  32. {
  33. if (!treasure.Contains(token[i]))
  34. {
  35. treasure.Insert(0, token[i]);
  36. }
  37. }
  38. }
  39.  
  40. else if (word == "Drop")
  41. {
  42. int index = int.Parse(token[1]);
  43. if (index >= 0 && index < treasure.Count)
  44. {
  45. string removedItem = treasure[index];
  46. treasure.RemoveAt(index);
  47. treasure.Add(removedItem);
  48. }
  49.  
  50. }
  51.  
  52. else if (word == "Steal")
  53. {
  54. int count = int.Parse(token[1]);
  55.  
  56. //if (count <= treasure.Count) // Check to see if gives Error in judge !!!
  57. if (count < treasure.Count)
  58. {
  59. for (int i = treasure.Count - count; i < treasure.Count; i++)
  60. {
  61. steal.Add(treasure[i]);
  62. }
  63.  
  64. Console.WriteLine(string.Join(", ", steal));
  65.  
  66. steal.Clear(); // !!! Clear the list
  67.  
  68. treasure.RemoveRange(treasure.Count - count, count);
  69. }
  70.  
  71. else
  72. {
  73. for (int i = 0; i < treasure.Count; i++)
  74. {
  75. steal.Add(treasure[i]);
  76. }
  77. Console.WriteLine(string.Join(", ", steal));
  78.  
  79. steal.Clear(); // !!! Clear the list
  80.  
  81. treasure.RemoveRange(0, treasure.Count);
  82. }
  83.  
  84. }
  85.  
  86. }
  87.  
  88. double sum = 0;
  89. double avg = 0;
  90.  
  91. foreach (var item in treasure)
  92. {
  93. sum = sum + item.Length;
  94. }
  95.  
  96.  
  97. if (treasure.Count != 0)
  98. {
  99. avg = sum / treasure.Count;
  100.  
  101. Console.WriteLine($"Average treasure gain: {avg:f2} pirate credits.");
  102. }
  103.  
  104. else
  105. {
  106. Console.WriteLine("Failed treasure hunt.");
  107. }
  108.  
  109. }
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment