bomb numbers
Здравейте,
условието е следното:
Write a program that reads sequence of numbers and special bomb number with a certain power. Your task is to detonate every occurrence of the special bomb number and according to its power his neighbors from left and right. Detonations are performed from left to right and all detonated numbers disappear. Finally print the sum of the remaining elements in the sequence.
бихте ли ми казали защо кодът ми не работи и къде бъркам, хваща всички нулеви тестове, но се дъни на останалите.
package com.company;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class ExerBombNumbers {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
List<Integer> numbers = Arrays.stream(scan.nextLine().split(" "))
.map(Integer::parseInt)
.collect(Collectors.toList());
String[] bombPower = scan.nextLine().split(" ");
int bomb = Integer.parseInt(bombPower[0]);
int power = Integer.parseInt(bombPower[1]);
int bombs = 0;
for (int i = 0; i <= numbers.size(); i++) {
if (numbers.contains(bomb)) {
bombs++;
}
}
//detonate
numbers = detonation(numbers, bomb, power);
for (int i = 0; i < numbers.size(); i++) {
if (numbers.get(i) == bomb){
numbers.remove(i);
i = 0;
}
}
//sum
int total = 0;
for (int i = 0; i < numbers.size(); i++) {
total += numbers.get(i);
// System.out.print(numbers.get(i) + " ");
}
System.out.println(/*"| " + */total);
}
public static List<Integer> detonation(List<Integer> mineField, int bomb, int power) {
for (int i = 0; i < mineField.size(); i++) {
if (mineField.get(i) == bomb) {
//removeRight
for (int r = i + 1; r <= i + power; r++) {
try {
mineField.remove(r);
} catch (Exception e) {
break;
}
}
//removeLeft
for (int l = i - 1; l >= i - power; l--) {
try {
mineField.remove(l);
} catch (Exception e) {
break;
}
}
}
}
return mineField;
}
}
поздрави,