Loading...

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

inaivanova1990 avatar inaivanova1990 33 Точки

Array modifier

Здравейте,

 

https://pastebin.com/PEQQRHTA

 

Не си виждам грешката, а judge не ми дава никакви точки. Нулевите тестове минават.

 

You are given an array with integers. Write a program to modify the elements after receiving the following commands:

  • takes two elements and swap their places.
  • takes element at the 1st index and multiply it with the element at 2nd index. Save the product at the 1st index.
  • decreases all elements in the array with 1.

Input

On the first input line, you will be given the initial array values separated by a single space.

On the next lines you will receive commands until you receive the command "end". The commands are as follow:

  •  
  •  
  •  

Output

The output should be printed on the console and consist of elements of the modified arrayseparated by a comma and a single space ", ".

Constraints

  • Elements of the array will be integer numbers in the range [-231...231]
  • Count of the array elements will be in the range [2...100]
  • Indexes will be always in the range of the array

Examples

Input

Output

Comments

23 -2 321 87 42 90 -123

swap 1 3

swap 3 6

swap 1 0

multiply 1 2

multiply 2 1

decrease

end

86, 7382, 2369942, -124, 41, 89, -3

23 -2 321 87 42 90 -123 – initial values

swap 1(-2) and 3(87) ▼

23 87 321 -2 42 90 -123

swap 3(-2) and 6(-123) ▼

23 87 321 -123 42 90 -2

swap 1(87) and 0(23) ▼

87 23 321 -123 42 90 -2

multiply 1(23) 2(321) = 7383 ▼

87 7383 321 -123 42 290 -2

multiply 2(321) 1(7383) = 2369943 ▼

87 7383 2369943 -123 42 90 -2

decrease – all - 1 ▼

86 7383 2369942 -124 41 89 -3

1 2 3 4

swap 0 1

swap 1 2

swap 2 3

multiply 1 2

decrease

end

1, 11, 3, 0

 

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

"decrease" decreases all elements in the array with 1.

;-)

function solve(input) {
    // Parse all values in the array to int values
    let initialArray = input[0]
    .split(' ')
    .map(Number);

    for (let i = 1; i < input.length; i++) {

        let [command, index1, index2] = input[i].split(' ');

        // Values need to be parsed from string to int
        index1 = Number(index1);
        index2 = Number(index2);

        if (command === 'swap') {
            let first = initialArray[index1];
            let second = initialArray[index2];
            initialArray[index1] = second;
            initialArray[index2] = first;
        }
        if (command === 'multiply') {
            let first = initialArray[index1];
            let second = initialArray[index2];
            initialArray[index1] = first * second;
        }
        if (command === 'decrease') {
            initialArray = initialArray.map(el => el - 1);

            // ERROR
            // Carefull !!! .join will transform array into a string
            // initialArray = initialArray.map(el => el - 1).join(', ');
        }
    }

    // console.log(initialArray)
    return initialArray.join(', ');
}

 

 

1
16/06/2022 19:30:18
inaivanova1990 avatar inaivanova1990 33 Точки

Дам, добре е конвертирането на данните да се случва в началото :Д

1
soixx avatar soixx 8 Точки

Едно решение за C# 

int[] numbers = Console.ReadLine().Split(" ", StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();

string command = Console.ReadLine();

while (command != "end")
{
    string[] commandInfo = command.Split(" ");
    string action = commandInfo[0];
   

    if (action == "swap")
    {
        int index1 = int.Parse(commandInfo[1]);
        int index2 = int.Parse(commandInfo[2]);
        int first = numbers[index1];
        int second = numbers[index2];
        numbers[index1] = second;
        numbers[index2] = first;

    }

    else if (action == "multiply")
    {
        int index1 = int.Parse(commandInfo[1]);
        int index2 = int.Parse(commandInfo[2]);
        int first = numbers[index1];
        int second = numbers[index2];
        numbers[index1] = first * second;

    }
    else if ( action == "decrease")
    {
        for (int i = 0; i < numbers.Length; i++)
        {
            numbers[i] -= 1;
      }
   }


    command = Console.ReadLine();
}

Console.WriteLine(string.Join(", ", numbers));

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