洛谷链接:https://www.luogu.com.cn/problem/P1165
这题对java很不友好,同一份代码,c就能AC,java就拿不了满分
49分代码
#include<bits/stdc++.h>
using namespace std;
stack<int>a;
stack<int>b;
int n,m,x;
int main()
{
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
if(m==0)
{
scanf("%d",&x);
a.push(x);
if(b.empty()||x>b.top())
b.push(x);
else b.push(b.top());
}
else if(m==1)
{
a.pop();
b.pop();
}
else
{
if(b.empty()) printf("0\n");
else printf("%d\n",b.top());
}
}
return 0;
}
83分代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] f=new int[200001];
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int t=0; //表示当前栈里数的个数
f[0]=0;
for(int i=1;i<=n;i++) {
int x=in.nextInt();
if(x==0) {
int y=in.nextInt();
t++;
f[t]=Math.max(f[t-1], y);
}
if(x==1) {
if(t!=0) {
t--;
}
}
if(x==2) {
System.out.println(f[t]);
}
}
}
}