+ Нов въпрос
LilianaVelvheva 8 Точки

## Къде ми е грешката в задачата Array modifier?

https://pastebin.com/Nr5Md0Fk

Това е моя код.

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

# 02. Array Modifier

You are given an array with integers. Write a program to modify the elements after receive the commandsswap”, “multiply” or “decrease”.

• “swap {index1} {index2}” take two elements and swap their places.
• “multiply {index1} {index2}” take element at the 1st index and multiply it with element at 2nd index. Save the product at the 1st index.
• “decrease” 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:

• “swap {index1} {index2}”
• “multiply {index1} {index2}”
• “decrease”

## Output

The output should be printed on the console and consist element of the modified arrayseparated by “, “(comma and single space).

## Constraints

• Commands will be: “swap”, “multiply”, “decrease” and “end
• 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

 Input JavaScript 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
Тагове:
1
krum_43 635 Точки

Здравей LilianaVelvheva,

Не знам какви точки ти присъжда съдията,но мисля че при размяната на стойностите на елементите си пропуснала да запазиш стойността на първият елмент в променливата temp.

temp=array{index1];

array[index1]=array[index2];

array[index2]=temp;

Eто и едно решение на C#:

https://pastebin.com/ihAeDnYS

0
20/07/2021 14:36:54