大鱼吃小鱼(简单模拟)

题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1289

从左往右将数字压入栈里(想象成一个水平向右的栈),如果鱼是向左的让它一直吃,直到被吃或者吃完为止

import java.util.Scanner;
import java.util.Stack;

public class N1289 {

    private static Scanner cin;

    public static void main(String[] args) {
        cin = new Scanner(System.in);
        int n=cin.nextInt(),ans=n,size,dir;
        Stack s=new Stack();
        
        for(int i=0;i)
        {
            size=cin.nextInt();
            dir=cin.nextInt();
            
            if(dir==1)
                s.push(new Integer(size));
            else
            {
                while(s.empty()!=true)
                {
                    if(size>Integer.parseInt(s.peek().toString()))
                    {
                        s.pop();
                        ans--;
                    }
                    else
                    {
                        ans--;
                        break;
                    }
                }
            }
        }
        System.out.println(ans);
    }
}

 

转载于:https://www.cnblogs.com/NDKY9/p/7535366.html

你可能感兴趣的:(java)