Skip to content
Closed
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions src/main/java/com/walking/intensive/chapter3/task12/Task12.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.walking.intensive.chapter3.task12;

import java.util.Arrays;
import java.util.Scanner;

/**
* Девочка Света очень любит играть в мячики. Она поставила в ряд корзинки и в некоторые положила по 1 мячику.
* За 1 раз она может переложить 1 мячик в соседнюю корзинку. В 1 корзинке может поместиться много мячиков.
Expand Down Expand Up @@ -40,11 +43,37 @@
*/
public class Task12 {
public static void main(String[] args) {
// Для собственных проверок можете делать любые изменения в этом методе
Scanner in = new Scanner(System.in);
System.out.print("Введите данные: ");
String baskets = in.nextLine();
in.close();

System.out.print(Arrays.toString(getMovementsNumber(baskets)));

}

static int[] getMovementsNumber(String baskets) {
// Ваш код
return new int[]{};
int basketsAmount = baskets.length();
int[] basketsArray = new int[basketsAmount];

for (int i = 0; i < basketsArray.length; i++) {
basketsArray[i] = Character.getNumericValue(baskets.charAt(i));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Стоит ли вообще тратить ресурсы на конвертацию символов в числа? Какая разница, валидировать 0 и 1 или '0' и '1'?

if (basketsArray[i] != 0 && basketsArray[i] != 1) {
return new int[]{};
}
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Логичнее было бы все, вместе с циклом, вынести в boolean-метод isValid(). И если результат false - возвращать пустой массив. Но текущий вариант тоже имеет право на жизнь


int[] actionsAmount = new int[basketsAmount];
for (int i = 0; i < actionsAmount.length; i++) {
for (int left = 0; left < i; left++) {
actionsAmount[i] += basketsArray[left] * (i - left);
}

for (int right = i + 1; right < actionsAmount.length; right++) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

есть четкое ощущение, что использование модуля (Math.abs()) избавит тебя от необходимости использовать два последовательных цикла и делить корзинки на правые и левые

actionsAmount[i] += basketsArray[right] * (right - i);
}
}

return actionsAmount;
}
}