Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package PodgotovkaZaIzpit;
- import java.util.Scanner;
- public class Pomosht {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String[] pricesStr = scanner.nextLine().split(", ");
- int[] cena = new int[pricesStr.length];
- for (int i = 0; i < cena.length; i++) {
- cena[i] = Integer.parseInt(pricesStr[i]);
- }
- int entryPoint = Integer.parseInt(scanner.nextLine());
- String targetType = scanner.nextLine();
- int leftDamage = damage(cena, entryPoint, targetType, true);
- int rightDamage = damage(cena, entryPoint, targetType, false);
- if (leftDamage >= rightDamage) {
- System.out.printf("Left - %d%n", leftDamage);
- } else {
- System.out.printf("Right - %d%n", rightDamage);
- }
- }
- private static int damage(int[] prices, int entryPoint, String targetType, boolean isLeft) {
- int damage = 0;
- int startIndex = isLeft ? entryPoint - 1 : entryPoint + 1;
- int endIndex = isLeft ? 0 : prices.length;
- int step = isLeft ? -1 : 1;
- for (int i = startIndex; isLeft ? i >= endIndex : i < endIndex; i += step) {
- if ((targetType.equals("cheap") && prices[i] < prices[entryPoint])
- || (targetType.equals("expensive") && prices[i] >= prices[entryPoint])) {
- damage += prices[i];
- }
- }
- return damage;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement