zoukankan      html  css  js  c++  java
  • [洛谷P2186] 小Z的栈函数

    题目链接:###

    传送门

    题目分析:###

    大模拟,先得存操作,然后再处理每个数……
    有一个小优化,在处理操作的时候顺便判一下最后栈里是不是有且仅有一个数,但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
    "); 
    		else printf("%lld
    ",sta[top]);
    	}
    	return 0;
    }
    
  • 相关阅读:
    面向对象程序设计简介(1/2)
    iOS官方Sample大全
    AFN不支持 "text/html" 的数据的问题:unacceptable content-type: text/html
    谈ObjC对象的两段构造模式
    关于self和super在oc中的疑惑与分析 (self= [super init])
    在Xcode中使用Git进行源码版本控制
    NSObject之二
    NSObject之一
    Objective-C Runtime 运行时之六:拾遗
    Objective-C Runtime 运行时之五:协议与分类
  • 原文地址:https://www.cnblogs.com/kma093/p/10327326.html
Copyright © 2011-2022 走看看