Didart

Hot Potato

Dec 27th, 2022
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. package StacksAndQueues;
  2.  
  3. import java.util.ArrayDeque;
  4. import java.util.Scanner;
  5.  
  6. public class HotPotato {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         String[] input = scanner.nextLine().split("\\s+");
  11.  
  12.         int number = Integer.parseInt(scanner.nextLine());
  13.  
  14.         ArrayDeque<String> queue = new ArrayDeque<>();
  15.  
  16.         for (int i = 0; i < input.length; i++) {
  17.             queue.offer(input[i]);
  18.         }
  19.  
  20.         while (queue.size() > 1) {
  21.            
  22.             for (int i = 0; i < number - 1; i++) {
  23.                 String name = queue.poll();
  24.                 queue.offer(name);
  25.             }
  26.             System.out.println("Removed " + queue.poll());
  27.         }
  28.        
  29.         for (String child : queue) {
  30.             System.out.println("Last is " + child);
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment