-
Notifications
You must be signed in to change notification settings - Fork 226
Task12 #813
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
Task12 #813
Changes from 8 commits
c83ea1a
2810432
8287262
0d3b71a
39521db
892435c
c9bfb7c
1a9a050
b6589e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 корзинке может поместиться много мячиков. | ||
|
|
@@ -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)); | ||
| if (basketsArray[i] != 0 && basketsArray[i] != 1) { | ||
| return new int[]{}; | ||
| } | ||
| } | ||
KFalcon2022 marked this conversation as resolved.
Show resolved
Hide resolved
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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++) { | ||
|
||
| actionsAmount[i] += basketsArray[right] * (right - i); | ||
| } | ||
| } | ||
|
|
||
| return actionsAmount; | ||
| } | ||
| } | ||
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.
Стоит ли вообще тратить ресурсы на конвертацию символов в числа? Какая разница, валидировать 0 и 1 или '0' и '1'?