SHARE
TWEET

Untitled

elena_veskova Dec 6th, 2019 (edited) 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2.  
  3. public class Task {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner scanner = new Scanner(System.in);
  8.         int pos = Integer.parseInt(scanner.nextLine());
  9.         String[] strArray = scanner.nextLine().split(",");
  10.         String input = scanner.nextLine();
  11.  
  12.         int time = 0;
  13.         String command = "";
  14.         int size = 0;
  15.  
  16.         long forward = 0;
  17.         long backwards = 0;
  18.         int counter = 1;
  19.         int currentPos = pos;
  20.  
  21.         int[] array = new int[strArray.length];
  22.         for (int i = 0; i < strArray.length; i++) {
  23.             array[i] = Integer.parseInt(strArray[i]);
  24.         }
  25.         while (!input.equals("exit")) {
  26.             String[] strCommand = input.split(" ");
  27.             time = Integer.parseInt(strCommand[0]);
  28.             command = strCommand[1];
  29.             size = Integer.parseInt(strCommand[2]);
  30.             if (command.equals("forward")) {
  31.                 while (counter <= time) {
  32.                     currentPos = currentPos + size;
  33.                     if (currentPos >= array.length) {
  34.                         currentPos = currentPos % array.length;
  35.                     }
  36.                     forward += array[currentPos];
  37.                     counter++;
  38.                 }
  39.                 counter = 1;
  40.             }
  41.             if (command.equals("backwards")) {
  42.                 while (counter <= time) {
  43.                     currentPos = currentPos - size + array.length;
  44.                     if (currentPos < 0 || currentPos >= array.length) {
  45.                         currentPos = Math.abs(currentPos % array.length);
  46.                     }
  47.                     backwards += array[currentPos];
  48.                     counter++;
  49.                 }
  50.                 counter = 1;
  51.             }
  52.             input = scanner.nextLine();
  53.         }
  54.         System.out.println("Forward: " + forward);
  55.         System.out.println("Backwards: " + backwards);
  56.     }
  57. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top