Skip to content
51 changes: 48 additions & 3 deletions src/main/java/com/walking/intensive/chapter3/task13/Task13.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.walking.intensive.chapter3.task13;

import java.util.Scanner;

import static java.lang.Integer.parseInt;

/**
* Ваша задача - с помощью лейки полить все растения в саду.
* Всего в саду N растений. Они расположены в ряд и слева направо помечены
Expand Down Expand Up @@ -49,11 +53,52 @@
*/
public class Task13 {
public static void main(String[] args) {
// Для собственных проверок можете делать любые изменения в этом методе

Scanner in = new Scanner(System.in);
System.out.println("Введите через пробел количество воды, необходимое для полива каждого растения: ");
String plantsWatering = in.nextLine();
System.out.println("Введите объём лейки: ");
int wateringCanVolume = in.nextInt();
in.close();

String[] plantsWater = plantsWatering.split(" ");
int[] plants = new int[plantsWater.length];
for (int i = 0; i < plants.length; i++) {
plants[i] = parseInt(plantsWater[i]);
}

System.out.println(getStepsCount(plants, wateringCanVolume));
}

static int getStepsCount(int[] plants, int wateringCanVolume) {
// Ваш код
return 0;
if (plants.length == 0) {
return 0;
}

if (wateringCanVolume < 1) {
return -1;
}

int stepsCount = 0;
int leftover = wateringCanVolume;
for (int i = 0; i < plants.length; i++) {
if (!isValid(plants[i], wateringCanVolume)) {
Copy link
Owner

Choose a reason for hiding this comment

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

валидация все еще не выдела из логики. Где гарантия, что на plants[plants.length - 1] мы не обнаружим не валидное число? Получится, что все итерации до этого затратили лишние ресурсы процессора

Copy link
Author

Choose a reason for hiding this comment

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

думала, лучше всё одним циклом сделать, теперь понимаю, почему не лучше 😄

return -1;
}

if (leftover < plants[i]) {
stepsCount += i * 2;
leftover = wateringCanVolume - plants[i];
}
stepsCount += 1;
Copy link
Owner

Choose a reason for hiding this comment

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

кажется, табуляция поплыла

leftover -= plants[i];

}

return stepsCount;
}

static boolean isValid (int plant, int wateringCanVolume) {
Copy link
Owner

Choose a reason for hiding this comment

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

лишний пробел после имени метода

return plant >= 1 && plant <= wateringCanVolume;
}
}
Loading