Advertisement
Georgi_Benchev

Untitled

Oct 10th, 2024
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. package Telerik_Alpha_Java_2024.CodingTasks_2;
  2.  
  3. import java.math.BigInteger;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6.  
  7. public class Task2_Move {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         int curentPosition = Integer.parseInt(scanner.nextLine());
  12.         int[] values = Arrays.stream(scanner.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();
  13.         BigInteger forwardCount = BigInteger.valueOf(0);
  14.         BigInteger backwardsCount = BigInteger.valueOf(0);
  15.  
  16.         String input = scanner.nextLine();
  17.  
  18.         while (!input.equals("exit")) {
  19.             String[] commands = input.split(" ");
  20.             int moves = Integer.parseInt(commands[0]);
  21.             String direction = commands[1];
  22.             int sizeOfMoves = Integer.parseInt(commands[2]);
  23.             sizeOfMoves = sizeOfMoves % values.length;
  24.  
  25.  
  26.             if (direction.equals("forward")) {
  27.                 for (int i = 0; i < moves; i++) {
  28.                     if (curentPosition + sizeOfMoves < values.length) {
  29.                         curentPosition += sizeOfMoves;
  30.                         forwardCount = forwardCount.add(BigInteger.valueOf(values[curentPosition]));
  31.                     } else {
  32.                         curentPosition = Math.abs(values.length - (sizeOfMoves + curentPosition));
  33.                         forwardCount = forwardCount.add(BigInteger.valueOf(values[curentPosition]));
  34.                     }
  35.                 }
  36.             } else if (direction.equals("backwards")) {
  37.                 for (int i = 0; i < moves; i++) {
  38.                     if (curentPosition - sizeOfMoves >= 0) {
  39.                         curentPosition -= sizeOfMoves;
  40.                         backwardsCount = backwardsCount.add(BigInteger.valueOf(values[curentPosition]));
  41.                     } else {
  42.                         curentPosition = values.length + (curentPosition - sizeOfMoves);
  43.                         backwardsCount = backwardsCount.add(BigInteger.valueOf(values[curentPosition]));
  44.                     }
  45.  
  46.                 }
  47.  
  48.  
  49.             }
  50.             input = scanner.nextLine();
  51.  
  52.         }
  53.         System.out.println("Forward: " + forwardCount);
  54.         System.out.println("Backwards: " + backwardsCount);
  55.     }
  56. }
  57.  
  58.  
  59. //4
  60. //10000,2000,3000,4000,5000
  61. //2000 forward 10000
  62. //2000 backwards 10000
  63. //3000 forward 2000
  64. //3000 backwards 2000
  65. //exit
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement