Advertisement
Krassi_Daskalova

Hot Potato

Jan 26th, 2022
936
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayDeque;
  2. import java.util.Scanner;
  3.  
  4. public class HotPotato {
  5.     public static void main(String[] args){
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         ArrayDeque<String> queueOfNames = new ArrayDeque<>();
  9.         String[] names = scanner.nextLine().split(" ");
  10.  
  11.         for (String name : names) {
  12.             queueOfNames.offer(name);
  13.         }
  14.         int num = Integer.parseInt(scanner.nextLine());
  15.  
  16.         while (queueOfNames.size() > 1){
  17.             for (int i = 1; i < num; i++){
  18.                 String rearrangeName = queueOfNames.poll();
  19.                 queueOfNames.offer(rearrangeName);
  20.             }
  21.             System.out.println("Removed "+ queueOfNames.poll());
  22.         }
  23.         System.out.println("Last is "+ queueOfNames.peek());
  24.     }
  25. }
  26.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement