Advertisement
LoraOrliGeo

Hot Potato - Stecks_Queues

May 17th, 2019
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Collections;
  3. import java.util.Scanner;
  4.  
  5. public class P6_HotPotato {
  6.     public static void main(String[] args) {
  7.         @SuppressWarnings("resource")
  8.  
  9.         Scanner sc = new Scanner(System.in);
  10.  
  11.         ArrayDeque<String> kids = new ArrayDeque<>();
  12.  
  13.         String[] names = sc.nextLine().split("\\s+");
  14.  
  15.         Collections.addAll(kids, names);
  16.  
  17. //      for (String name : names) {
  18. //          kids.offer(name);
  19. //      }
  20.  
  21.         int n = Integer.parseInt(sc.nextLine());
  22.  
  23.         while (kids.size() > 1) {
  24.             for (int i = 1; i < n ; i++) {
  25.                 String rearrangeKid = kids.poll();
  26.                 kids.offer(rearrangeKid);
  27.             }
  28.            
  29.             System.out.println("Removed " + kids.poll());
  30.         }
  31.  
  32.         System.out.println("Last is " + kids.peek());
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement