[洛谷P2186] 小Z的棧函數
阿新 • • 發佈:2019-01-27
www. 博客 mes 題目 digi lld 分析 傳送門 http
題目鏈接:
傳送門
題目分析:
大模擬,先得存操作,然後再處理每個數……
有一個小優化,在處理操作的時候順便判一下最後棧裏是不是有且僅有一個數,但A完了才想起來,所以就算了……
總之就是個模擬題……沒什麽算法,但細節很多我就是來水個博客
代碼:
#include<bits/stdc++.h> using namespace std; inline long long read(){ int cnt=0,f=1;char c; c=getchar(); while(!isdigit(c)){ if(c==‘-‘)f=-f; c=getchar(); } while(isdigit(c)){ cnt=cnt*10+c-‘0‘; c=getchar(); } return cnt*f; } int n; char s[200]; int opr[20005],tot=0; long long sta[20005]; long long x,top=1; long long a[20005],u=0; long long p=-1; bool flag=false; bool check1(){ if(u>1000000000||-u>1000000000)return false; else return true; } bool check2(){ if(top>1||top==0)return false; else return true; } int main(){ memset(opr,0,sizeof(opr)); memset(a,128,sizeof(a)); while(1){ scanf("%s",s+1); if(s[1]==‘E‘)break; else { if(s[1]==‘N‘&&s[2]==‘U‘){ opr[++tot]=-10; a[tot]=read(); } if(s[1]==‘P‘)opr[++tot]=2; if(s[1]==‘I‘)opr[++tot]=3; if(s[1]==‘D‘&&s[2]==‘U‘)opr[++tot]=4; if(s[1]==‘S‘&&s[2]==‘W‘)opr[++tot]=5; if(s[1]==‘A‘)opr[++tot]=6; if(s[1]==‘S‘&&s[2]==‘U‘)opr[++tot]=7; if(s[1]==‘M‘&&s[2]==‘U‘)opr[++tot]=8; if(s[1]==‘D‘&&s[2]==‘I‘)opr[++tot]=9; if(s[1]==‘M‘&&s[2]==‘O‘)opr[++tot]=10; } } n=read(); while(n--){ flag=false; top=0; x=read(); sta[++top]=x; for(register int i=1;i<=tot;i++){ if(opr[i]==-10){ sta[++top]=a[i]; if(a[i]>1000000000||-a[i]>1000000000){flag=true;break;} } if(opr[i]==2){ if(!top)flag=true; else top--; } if(opr[i]==3){ if(!top)flag=true; else sta[top]=-sta[top]; } if(opr[i]==4){ if(!top)flag=true; else{ ++top; sta[top]=sta[top-1]; } } if(opr[i]==5){ if(!top||top==1)flag=true; else swap(sta[top],sta[top-1]); } if(opr[i]==6){ if(!top||top==1)flag=true; else{ u=sta[top]+sta[top-1]; sta[--top]=u; if(!check1())flag=true; } } if(opr[i]==7){ if(!top||top==1)flag=true; else{ u=sta[top-1]-sta[top]; sta[--top]=u; } if(!check1())flag=true; } if(opr[i]==8){ if(!top||top==1)flag=true; else{ u=sta[top-1]*sta[top]; sta[--top]=u; } if(!check1())flag=true; } if(opr[i]==9){ if(!top||top==1||sta[top]==0)flag=true; else{ u=sta[top-1]/sta[top]; sta[--top]=u; } if(!check1())flag=true; } if(opr[i]==10){ if(!top||top==1)flag=true; else{ u=sta[top-1]%sta[top]; sta[--top]=u; } if(!check1())flag=true; } } if(!check2())flag=true; if(!check1())flag=true; if(flag)printf("ERROR\n"); else printf("%lld\n",sta[top]); } return 0; }
[洛谷P2186] 小Z的棧函數