Advertisement
Martina312

Asteroid Collision

Feb 5th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.32 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class AsteroidColision {
  4.     public static  void collision(Stack<Integer> stack){
  5.         ArrayStack<Integer> pom=new ArrayStack<>(100);
  6.         while(!stack.isEmpty()){
  7.             int num1=stack.pop();
  8.             if(stack.isEmpty()) {
  9.                 stack.push(num1);
  10.                 break;
  11.             }
  12.             int num2=stack.pop();
  13.  
  14.             if((num1>0 && num2<0) || (num1<0 && num2>0)){
  15.                 if(Math.abs(num1) > Math.abs(num2)){
  16.                     stack.push(num1);
  17.                 }
  18.                 else if(Math.abs(num1) < Math.abs(num2)){
  19.                     stack.push(num2);
  20.                 }
  21.             }
  22.             else{
  23.                 stack.push(num2);
  24.                 pom.push(num1);
  25.             }
  26.  
  27.         }
  28.  
  29.         while (!stack.isEmpty()){
  30.            pom.push(stack.pop());
  31.         }
  32.         if (pom.isEmpty())
  33.             System.out.println("none");
  34.         while (!pom.isEmpty()){
  35.             System.out.println(pom.pop());
  36.         }
  37.     }
  38.     public static void main(String[] args) {
  39.         Scanner in=new Scanner(System.in);
  40.         int n=in.nextInt();
  41.  
  42.         ArrayStack<Integer> stack=new ArrayStack<>(100);
  43.         for(int i=0;i<n;i++){
  44.             stack.push(in.nextInt());
  45.         }
  46.  
  47.         collision(stack);
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement