Advertisement
deyanmalinov

6. Hot Potato

Mar 23rd, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.         String[] line = scan.nextLine().split("\\s+");
  8.         int num = Integer.parseInt(scan.nextLine());
  9.         ArrayDeque<String> queue = new ArrayDeque<>();
  10.         for (int i = 0; i < line.length; i++) {
  11.             queue.offer(line[i]);
  12.         }
  13.         while (queue.size() > 1){
  14.                 for (int i = 0; i < num-1; i++) {
  15.                     String name = queue.poll();
  16.                     queue.offer(name);
  17.                 }
  18.             System.out.println("Removed "+queue.poll());
  19.         }
  20.             for (String s : queue) {
  21.             System.out.println("Last is " + s);
  22.         }
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement