Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class Program
  5. {
  6. public static void Main()
  7. {
  8. // 4 2
  9. // expected [1, 2, 9] with sum 12;
  10. List<int> list = new List<int>() { 1, 2, 2, 4, 2, 2, 2, 9 };
  11. detonateBomb(list, 4, 2);
  12.  
  13. // 9 3
  14. // expected [1, 4] with sum 5;
  15. list = new List<int>() { 1, 4, 4, 2, 8, 9, 1 };
  16. detonateBomb(list, 9, 3);
  17.  
  18. // 7 1
  19. // expected [1, 2, 3] with sum 6;
  20. list = new List<int>() { 1, 7, 7, 1, 2, 3 };
  21. detonateBomb(list, 7, 1);
  22.  
  23. // 2 1
  24. // expected [1, 1, 1, 1] with sum 4;
  25. list = new List<int>() { 1, 1, 2, 1, 1, 1, 2, 1, 1, 1 };
  26. detonateBomb(list, 2, 1);
  27. }
  28.  
  29. public static void detonateBomb(List<int> list, int specialNumber, int power)
  30. {
  31. printTheList(list);
  32. Console.WriteLine("Special number: " + specialNumber + ", power: " + power);
  33.  
  34. // 1, 2, 2, 4, 2, 2, 2, 9 - 8.count
  35. for (int index = 0; index < list.Count; index++)
  36. {
  37.  
  38. if (specialNumber == list[index])
  39. {
  40. // define the lower border
  41. int lowerBorder = index - power;
  42. if (lowerBorder < 0)
  43. {
  44. lowerBorder = 0;
  45. }
  46.  
  47. // define the upper border
  48. int upperBorder = index + power;
  49. if (upperBorder >= list.Count)
  50. {
  51. upperBorder = list.Count - 1;
  52. }
  53.  
  54. int numberOfElementsToRemove = upperBorder - lowerBorder;
  55.  
  56. // remove the elements on the left, detonate the bomb and
  57. // remove the elements on the right
  58. // less than or equal to the number, since the last element should be removed inclusive
  59. for (int i = 0; i <= numberOfElementsToRemove; i++)
  60. {
  61. list.RemoveAt(lowerBorder);
  62. }
  63. }
  64. }
  65.  
  66. printTheListAndTheSum(list);
  67. }
  68.  
  69. public static void printTheList(List<int> list)
  70. {
  71. Console.Write("[");
  72.  
  73. for (int i = 0; i < list.Count; i++)
  74. {
  75. Console.Write(list[i]);
  76.  
  77. if (i < list.Count - 1)
  78. {
  79. Console.Write(", ");
  80. }
  81. }
  82.  
  83. Console.WriteLine("]");
  84. }
  85.  
  86. public static void printTheListAndTheSum(List<int> list)
  87. {
  88. printTheList(list);
  89.  
  90. int sum = 0;
  91.  
  92. for (int i = 0; i < list.Count; i++)
  93. {
  94. sum += list[i];
  95. }
  96.  
  97. Console.WriteLine("The sum is " + sum + "\n");
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement