1 solutions

  • 0
    @ 2024-12-10 22:16:30

    C++ :

    #include<iostream>
    #include<stack>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    
    string s;
    char x[200];
    int sz[200],t=0,r=0;
    stack<char> fuhao;
    
    void work(string s);
    bool check(string s);
    
    int main()
    {
    	//freopen("test.in","r",stdin);
    	int z,j1,j2;
    	cin>>s;
    	if (check(s))
    	{
    		work(s);
    		for (int i=0;i<t;i++)
    	    {
    		  if(x[i]<'0' && x[i]!=' ')
    	  	  {
    			z=i-1; 
    			while (x[z]==' ') z--;
    			j1=sz[x[z]-'0'];
    			x[z]=' ';
    			z=i-2; 
    			while (x[z]==' ') z--;
    			j2=sz[x[z]-'0'];
    			x[z]=' ';
    			
    			if (x[i]=='+') 
    			{
    				sz[r]=j1+j2;
    				x[i]='0'+r;
    				r++;
    			}
    			else if (x[i]=='-') 
    			{
    				sz[r]=j2-j1;
    				x[i]='0'+r;
    				r++;
    			}
    			else if (x[i]=='*') 
    			{
    				sz[r]=j1*j2;
    				x[i]='0'+r;
    				r++;
    			}
    			else if (x[i]=='/') 
    			{
    				sz[r]=j2/j1;
    				x[i]='0'+r;
    				r++;
    			}
    		  }
    	    }
    	cout<<sz[r-1];
    	}
    	else cout<<"NO";	
    	
    	return 0;
    }
    
    bool check(string s)
    {
    	int i=0,kuohao=0;
    	while (s[i]!='@')
    	{
    		if (s[i]=='(') kuohao++;
    		if (s[i]==')' && kuohao==0) return false;
    		else kuohao--;
    		if (i==0 && (s[i]=='*' || s[i]=='/')) return false;
    		if (s[i]<'0' && s[i+1]<'0' && s[i]>='*' && s[i+1]>='*') return false;
    		if (s[i]=='0' && s[i+1]=='0') return false;
    		if (s[i]=='/' && s[i]=='0') return false;
    		i++;
    	}
    	if (s[i-1]<'0' && s[i-2]<'0') return false;
    	return true;
    }
    
    void work(string s)
    {
    	int i=0;
    	while (s[i]!='@')
    	{
    		if (s[i]=='+' || s[i]=='-')
    		{
    			 while (!fuhao.empty() && fuhao.top()!='(')
    			 {
    				 x[t]=fuhao.top();
    				 t++;
    				 fuhao.pop();
    			 }
    			fuhao.push(s[i]);
    			i++;
    			continue;
    		}
    		
    		if (s[i]=='*' || s[i]=='/')
    		{
    			 while (!fuhao.empty() && (fuhao.top()=='*' || fuhao.top()=='/'))
    			 {
    			 	 x[t]=fuhao.top();
    				 t++;
    				 fuhao.pop();
    			 }
    			 fuhao.push(s[i]);
    			 i++;
    			 continue;
    		}
    		
    		if (s[i]=='(') 
    		{
    			fuhao.push(s[i]);
    			i++;
    			continue;
    		}
    		
    		if (s[i]==')')
    		{
    			while (fuhao.top()!='(')
    			{
    				x[t]=fuhao.top();
    				t++;
    				fuhao.pop();
    			}
    			fuhao.pop();
    			i++;
    			continue;
    		}
    		
    		else 
    		{
    			while(s[i]>='0' && s[i]<='9')
    			{
    				sz[r]=sz[r]*10+s[i]-'0';
    				i++;
    			}
    			x[t]='0'+r;
    			t++; r++;
    		}
    	}
    	while (!fuhao.empty())
    	{
    		x[t]=fuhao.top();
    		t++;
    		fuhao.pop();
    	}
    }
    
    • 1

    Information

    ID
    526
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By