-
Notifications
You must be signed in to change notification settings - Fork 226
Task13 #817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stillwearit
wants to merge
15
commits into
KFalcon2022:master
Choose a base branch
from
stillwearit:task13
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Task13 #817
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c83ea1a
?
stillwearit 2810432
готово?
stillwearit 49b2ea7
пробуем
stillwearit b3d21f0
пробуем
stillwearit 2e19034
пробуем
stillwearit 28ad386
пробуем
stillwearit c9be951
пробуем
stillwearit b597da9
пробуем
stillwearit e03bd19
пробуем
stillwearit e10d5e7
пробуем
stillwearit 6d16e3f
пробуем
stillwearit 5bf0c5c
пробуем
stillwearit 73f9f5c
пробуем
stillwearit fa55cfb
исправила
stillwearit 08a917e
готово
stillwearit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 растений. Они расположены в ряд и слева направо помечены | ||
|
|
@@ -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)) { | ||
| return -1; | ||
| } | ||
|
|
||
| if (leftover < plants[i]) { | ||
| stepsCount += i * 2; | ||
| leftover = wateringCanVolume - plants[i]; | ||
| } | ||
| stepsCount += 1; | ||
|
||
| leftover -= plants[i]; | ||
|
|
||
| } | ||
|
|
||
| return stepsCount; | ||
| } | ||
|
|
||
| static boolean isValid (int plant, int wateringCanVolume) { | ||
|
||
| return plant >= 1 && plant <= wateringCanVolume; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
валидация все еще не выдела из логики. Где гарантия, что на plants[plants.length - 1] мы не обнаружим не валидное число? Получится, что все итерации до этого затратили лишние ресурсы процессора
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
думала, лучше всё одним циклом сделать, теперь понимаю, почему не лучше 😄