Loading...

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

bparvanova avatar bparvanova 7 Точки

9. Pokemon Dont Go - C#

Здравейте,

Бихте ли ми помогнали със задачата по-долу.

В Judge ми дава 70/100, опитвам се да оправя кода и проверките, но не успявам.

Ето го кода ми: https://pastebin.com/0wT0JQsK

 

Условие на задачата:

9.*Pokemon Don’t Go

Ely likes to play Pokemon Go a lot. But Pokemon Go bankrupted … So the developers made Pokemon Don’t Go out of depression. And so Ely now plays Pokemon Don’t Go. In Pokemon Don’t Go, when you walk to a certain pokemon, those closer to you, naturally get further, and those further from you, get closer.

You will receive a sequence of integers, separated by spaces – the distances to the pokemons. Then you will begin receiving integers, which will correspond to indexes in that sequence.

When you receive an index, you must remove the element at that index from the sequence (as if you’ve captured the pokemon).

  • You must increase the value of all elements in the sequence, which are less or equal to the removed element, with the value of the removed element.
  • You must decrease the value of all elements in the sequence, which are greater than the removed element, with the value of the removed element.

If the given index is less than 0remove the first element of the sequence, and copy the last element to its place.

If the given index is greater than the last index of the sequenceremove the last element from the sequence, and copy the first element to its place.

The increasing and decreasing of elements should be done in these cases, also. The element, whose value you should use is the removed element.

The program ends when the sequence has no elements (there are no pokemons left for Ely to catch).

Input

  • On the first line of input you will receive a sequence of integersseparated by spaces.
  • On the next several lines you will receive integers – the indexes.

Output

  • When the program ends, you must print the summed value of all removed elements.

Constrains

  • The input data will consist only of valid integers in the range [-2.147.483.648, 2.147.483.647].

Examples

Input

Output

Comments

4 5 3

1

1

0

14

The array is {4, 5, 3}. The index is 1.

We remove 5, and we increase all the lower ones and decrease all higher ones.

In this case there are no higher than 5.

The result is {9, 8}.

The index is 1. So we remove 8, and decrease all the higher ones.

The result is {1}.

The index is 0. So we remove 1.

There are no elements left, so we print the sum of all removed elements.

5 + 8 + 1 = 14.

5 10 6 3 5

2

4

1

1

3

0

0

51

Step 1: {11, 4, 9, 11}

Step 2: {22, 15, 20, 22}

Step 3: {7, 5, 7}

Step 4: {2, 2}

Step 5: {4, 4}

Step 6: {8}

Step 7: {} (empty).

Result = 6 + 11 + 15 + 5 + 2 + 4 + 8 = 51.

 

 

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

Good code again, just some invalid indexation at some places which probably caused the failed test results:

                if (index < 0)
                {
                    removedElementValue = input[0];
                    sumRemovedElements += removedElementValue;
                    //input[0] = input[lastElementInput]; lastElementInput not valid index of given List collection
                    // input[0] = lastElementInput;
                    input[0] = input[input.Count - 1];
                    IncreaseDecrease(input, removedElementValue);
                }

                else if (index >= input.Count)
                {
                    //removedElementValue = input[lastElementInput]; lastElementInput not valid index of given List collection
                    // removedElementValue = lastElementInput;
                    removedElementValue = input[input.Count - 1];
                    sumRemovedElements += removedElementValue;
                    //input[lastElementInput] = input[0]; lastElementInput not valid index of given List collection
                    input[input.Count - 1] = input[0];
                    IncreaseDecrease(input, removedElementValue);
                }

 

Demo code:

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

namespace pokemonDontGo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> input = Console.ReadLine().Split().Select(int.Parse).ToList();
            int command = int.Parse(Console.ReadLine());
            int totalSum = 0;

            while (input.Count != 0)
            {
                bool skip = false;
                int pokemonDistance = 0;
                if (command < 0)
                {
                    pokemonDistance = input[0];
                    totalSum += pokemonDistance;
                    skip = true;
                    input.RemoveAt(0);
                    input.Insert(0, input[input.Count - 1]);
                }

                if (command > input.Count - 1)
                {
                    pokemonDistance = input[input.Count - 1];
                    totalSum += pokemonDistance;
                    skip = true;
                    input.RemoveAt(input.Count - 1);
                    input.Add(input[0]);
                }

                if (skip == false)
                {
                    pokemonDistance = input[command];
                    totalSum += pokemonDistance;
                    input.RemoveAt(command);
                }
                
                if (input.Count == 0)
                {
                    break;
                }

                for (int i = 0; i < input.Count; i++)
                {
                    if (input[i] <= pokemonDistance)
                    {
                        input[i] = input[i] + pokemonDistance;
                    }
                    else if (input[i] > pokemonDistance)
                    {
                        input[i] = input[i] - pokemonDistance;
                    }
                }

                command = int.Parse(Console.ReadLine());
            }

            Console.WriteLine(totalSum);
        }
    }
}

 Best,

1
bparvanova avatar bparvanova 7 Точки

Thanks!

I've fixed the indexes but still geting one incorrect test.

1
krum_43 avatar krum_43 750 Точки

Решението ти е добро.Както е коментирал колегата ако замениш 

 input[0] = input[lastElementInput]; със  input[0] = lastElementInput      и

 input[lastElementInput] = input[0]; със  input[input.count-1]= input[0];

може би ще вземеш100/100.

1
17/02/2021 10:14:58
bparvanova avatar bparvanova 7 Точки

Благодаря!

 

Оправих ги, но пак ми гърми на 7-ми тест. Явно не ми е било писано с тази задача :)

0
Axiomatik avatar Axiomatik 2422 Точки

Strange, when I submit your solution at judge I get 100%. Maybe you just had a time-out error, which usually gets resolved by itself when submitting again.

Code:


using System;
using System.Collections.Generic;
using System.Linq;
 
namespace _9_Pokemon_Don_t_Go
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> input = Console.ReadLine()
                .Split()
                .Select(int.Parse)
                .ToList();
 
            int sumRemovedElements = 0;
 
            while (input.Count > 0)
            {
                int index = int.Parse(Console.ReadLine());
 
                int lastElementInput = input[input.Count - 1];

                int removedElementValue = 0;
 
                if (index < 0)
                {
                    removedElementValue = input[0];
                    sumRemovedElements += removedElementValue;
                    input[0] = input[input.Count - 1];
                    IncreaseDecrease(input, removedElementValue);
                }
 
                else if (index >= input.Count)
                {
                    removedElementValue = input[input.Count - 1];
                    sumRemovedElements += removedElementValue;
                    input[input.Count - 1] = input[0];
                    IncreaseDecrease(input, removedElementValue);
                }
 
                else
                {
                    removedElementValue = input[index];
                    sumRemovedElements += removedElementValue;
                    input.RemoveAt(index);
                    IncreaseDecrease(input, removedElementValue);
                }
            }
 
            Console.WriteLine($"{sumRemovedElements}");
 
        }
 
        private static void IncreaseDecrease(List<int> input, int removedElementValue)
        {
            for (int i = 0; i < input.Count; i++)
            {
                if (input[i] <= removedElementValue)
                {
                    input[i] += removedElementValue;
                }
                else
                {
                    input[i] -= removedElementValue;
                }
            }
        }
    }
}

 

1
krum_43 avatar krum_43 750 Точки

При мен също дава 100/100.

0
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.