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

Проблем със задача Shoot for the Win от Programming Fundamentals Mid Exam Retake - 07 April 2020

Здравейте!

Не мога да реша тази задача, знам,че грешката е в начина, по който смята програмата, но не мога да я оправя, моля за помощ!

Кода ми: https://pastebin.com/Y93U1PZv

Условие:

Problem 2. Shoot for the Win

Write a program that helps you keep track of your shot targets. You will receive a sequence with integers, separated by single space, representing targets and their value. Afterwards, you will be receiving indices until the "End" command is given and you need to print the targets and the count of shot targets.

Every time you receive an index, you need to shoot the target on that index, if it is possiblie.

Everytime you shoot a target, its value becomes -1 and it is considered shot. Along with that you also need to:

  • Reduce all the other targets, which have greater values than your current target, with its value.
  • All the targets, which have less than or equal value to the shot target, you need to increase with its value.

Keep in mind that you can't shoot a target, which is already shot. You also can't increase or reduce a target, which is considered shot.

When you receive the "End" command, print the targets in their current state and the count of shot targets in the following format:

"Shot targets: {count} -> {target1} {target2}… {targetn}"

Input / Constraints

  • On the first line of input you will receive a sequence of integers, separated by a single space – the targets sequence.
  • On the next lines, until the "End" command you be receiving integers each on a single line – the index of the target to be shot.

Output

  • The format of the output is described above in the problem description.

Examples

Input

Output

Comments

24 50 36 70

0

4

3

1

End

Shot targets 3 -> -1 -1 130 -1

First we shoot target on index 0. It becomes equal to -1 and we start going through the rest of the targets. Since 50 is more than 24, we reduce it to 26 and 36 to 12 and 70 to 46. The sequence looks like that:

-1 26 12 46

The next index is invalid, so we don't do anything. Index 3 is valid and after the operations our sequence should look like that:

-1 72 58 -1

Then we take the first index with value 72 and our sequence looks like that:

-1 -1 130 -1

Then we print the result after the "End" command.

30 30 12 60 54 66

5

2

4

0

End

Shot targets: 4 -> -1 120 -1 66 -1 -1

 

 

Тагове:
0
Fundamentals Module
krasizorbov avatar krasizorbov 548 Точки
Best Answer

Ето задачата с малки поправки:

Успех!

using System;
using System.Linq;

namespace ConsoleApp1
{
    class Exam
    {
        static void Main(string[] args)
        {
            int[] targets = Console.ReadLine().Split().Select(int.Parse).ToArray();

            string command = "";

            int counter = 0;

            while ((command = Console.ReadLine()) != "End")
            {

                int indexTarget = int.Parse(command);

                if (indexTarget >= 0 && indexTarget < targets.Length)
                {
                    for (int i = 0; i < targets.Length; i++)
                    {
                        int temp = targets[indexTarget];

                        if (targets[i] != -1 && i != indexTarget)
                        {
                            if(targets[i] > temp)
                            {
                                targets[i] -= temp;
                            }
                            else if (targets[i] <= temp)
                            {
                                targets[i] += temp;

                            }
                        }
                    }
                    targets[indexTarget] = -1;
                    counter++;
                }
            }

            Console.WriteLine($"Shot targets: {counter} ->" + " " + string.Join(' ', targets));
        }
    }
}

0