desislava_topuzakova

02. Basic Stack Operations

May 21st, 2021
774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Collections;
  3. import java.util.Scanner;
  4. import java.util.Stack;
  5.  
  6. public class demo {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         //N, S, X
  10.         int n = scanner.nextInt(); //колко елемента искам да добавя -> колко пъти ще изпълня push
  11.         int s = scanner.nextInt();
  12.         int x = scanner.nextInt();
  13.  
  14.         ArrayDeque<Integer> stack = new ArrayDeque<>();
  15.         //n на брой пъти добавяме
  16.         for (int count = 1; count <= n; count++) {
  17.             stack.push(scanner.nextInt());
  18.         }
  19.  
  20.         //s на брой пъти pop
  21.         for (int count = 1; count <= s; count++) {
  22.             stack.pop();
  23.         }
  24.  
  25.         //x число, за което проверяваме дали е в стека
  26.         if (stack.contains(x)) {
  27.             System.out.println("true");
  28.         } else {
  29.             if (!stack.isEmpty()) {
  30.                 System.out.println(Collections.min(stack));
  31.             } else {
  32.                 System.out.println(0);
  33.             }
  34.         }
  35.  
  36.     }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment