Професионална програма
Loading...
Swimsas avatar Swimsas 3 Точки

05. Bomb Numbers Lists - Exercise

Колеги , моля за помощ всички четири теста от условието ги минавам , а джъджа ме чупи на всички тестове без зеро тестовете

ето кода ми - https://pastebin.com/8fDtjEtm

ето и условието 

1.Bomb Numbers

Write a program that reads a sequence of numbers and a 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.

Examples

Input

Output

Comments

1 2 2 4 2 2 2 9

4 2

12

Special number is 4 with power 2. After detontaion we are left with the sequence [1, 2, 9] with sum 12.

1 4 4 2 8 9 1

9 3

5

Special number is 9 with power 3. After detontaion we are left with the sequence [1, 4] with sum 5. Since the 9 has only 1 neighbour from the right we remove just it (one number instead of 3).

1 7 7 1 2 3

7 1

6

Detonations are performed from left to right. We cannot detonate the second occurance of 7, because its already destroyed by the first occurance. The numbers [1, 2, 3] survive. Their sum is 6.

1 1 2 1 1 1 2 1 1 1

2 1

4

The red and yellow numbers disappear in two sequential detonations. The result is the sequence [1, 1, 1, 1]. Sum = 4.

 

Тагове:
1
C# Fundamentals
Axiomatik avatar Axiomatik 1244 Точки

Having two nested for-loops inside of another for-loop will make your code just more tricky, mostly because the size of your collection diminishes, resulting in numbers being missed out. Check out the default solution with RemoveRange() and see if you can make your implementation work if you still want to use nested loops.

Best,

using System;
using System.Linq;
using System.Collections.Generic;

namespace _05.BombNumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> numbers = Console.ReadLine()
                .Split(' ', StringSplitOptions.RemoveEmptyEntries)
                .Select(int.Parse)
                .ToList();

            List<int> line = Console.ReadLine()
                .Split(' ', StringSplitOptions.RemoveEmptyEntries)
                .Select(int.Parse)
                .ToList();

            int bomb = line[0];
            int power = line[1];

            for (int i = 0; i < numbers.Count; i++)
            {
                if (numbers[i] == bomb)
                {
                    int lowerRange = i - power;
                    int count = 1 + (power * 2);

                    if (lowerRange < 0)
                    {
                        lowerRange = 0;
                        count--;
                    }

                    if (i >= numbers.Count - 1 || (count + lowerRange) - 1 >= numbers.Count)
                    {
                        count = numbers.Count - lowerRange;
                    }

                    numbers.RemoveRange(lowerRange, count);

                    i = -1;
                    // important to set back i -= 1, in order to not miss out on a given position
                    // see in Debug regime how code behaves



                    //numbers.RemoveAt(i);

                    //for (int k = i; k < i + power; k++)
                    //{
                    //    if (k >= numbers.Count)
                    //    {
                    //        break;
                    //    }
                    //    numbers.RemoveAt(k);
                    //}

                    //for (int j = i - power; j < i; j++)
                    //{
                    //    if (j < 0)
                    //    {
                    //        continue;
                    //    }
                    //    numbers.RemoveAt(j);
                    //    //numbers.RemoveAt(j--);
                    //    //i--;
                    //}

                }
            }

            Console.WriteLine(numbers.Sum());
        }
    }
}

 

1