deyanmalinov

03. "Voina" - Number game

May 27th, 2019
143
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class Main {
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.         Set<Integer> firstPlayer = getCollect(scan.nextLine());
  8.         Set<Integer> secondPlayer = getCollect(scan.nextLine());
  9.  
  10.         for (int i = 0; i < 50; i++) {
  11.             if (firstPlayer.isEmpty() || secondPlayer.isEmpty()) break;
  12.  
  13.             Iterator<Integer> fPlaIter = firstPlayer.iterator();
  14.             Iterator<Integer> sPlaIter = secondPlayer.iterator();
  15.  
  16.             int firPVal = fPlaIter.next();
  17.             firstPlayer.remove(firPVal);
  18.             int secPVal = sPlaIter.next();
  19.             secondPlayer.remove(secPVal);
  20.  
  21.             if (firPVal > secPVal){
  22.                 firstPlayer.add(firPVal);
  23.                 firstPlayer.add(secPVal);
  24.             }else if (secPVal > firPVal){
  25.                 secondPlayer.add(firPVal);
  26.                 secondPlayer.add(secPVal);
  27.             }else {
  28.                 firstPlayer.add(firPVal);
  29.                 secondPlayer.add(secPVal);
  30.             }
  31.         }
  32.         if (firstPlayer.size() > secondPlayer.size()){
  33.             System.out.println("First player win!");
  34.         }else if (secondPlayer.size() > firstPlayer.size()){
  35.             System.out.println("Second player win!");
  36.         }else {
  37.             System.out.println("Draw");
  38.         }
  39.     }
  40.  
  41.     static LinkedHashSet<Integer> getCollect(String line) {
  42.        LinkedHashSet<Integer> res = new LinkedHashSet<>();
  43.        Arrays.stream(line.split(" ")).mapToInt(Integer::parseInt).forEach(res::add);
  44.        return res;
  45.     }
  46. }
RAW Paste Data