1 条题解

  • 0
    @ 2026-8-24 21:14:02
    #include <bits/stdc++.h>
    using namespace std;
    const int N=1005;
    char s[N];
    stack<char>fun;//函数符号栈;
    stack<int>num;//数值栈;
    int main()
    {
    	freopen("ex.in","r",stdin);	
    	freopen("ex.out","w",stdout);
        int T;
        scanf("%d",&T);
        while(T--)
        {
    		while(fun.size())fun.pop();
    		while(num.size())num.pop();
            scanf("%s",s);
            int len=strlen(s);
            int fu=1;//正负符号 
            for(int i=0; i<len; i++)
            {
                if(isalpha(s[i])&&isalpha(s[i+1])&&isalpha(s[i+2]))
                {
                    i+=2;//取函数的第三个字符入栈;max min add 用 x n d 代表 
                    fun.push(s[i]);
                }
                else if(s[i]=='-')
                {
                	fu=-1;
    			}			
    			else if(s[i]>='0'&&s[i]<='9')
                {
                    int x=0, j;
                    for(j=i; s[j]>='0'&&s[j]<='9'; j++)
                    {
                        x=x*10+(s[j]-'0');//算出数值;
                    }
                    i=j-1;
                    num.push(fu*x);//入栈;
                    fu=1;//恢复正号 
                }
                else if(s[i]==')')
                {
                	char c=fun.top(); fun.pop();
                	
                	int x,y;
                    x=num.top(); num.pop();
                    y=num.top(); num.pop();
                	
                    if(c=='d')//add;
                    {
                        num.push(x+y);
                    }
                    else if(c=='n')//min
                    { 
                        num.push(min(x,y));//算出数值还要入栈一次;                    
                    }
                    else if(c=='x')//max
                    {
                        num.push(max(x,y));
                    }
                }
            }
            printf("%d\n",num.top());
            num.pop();
        }
        return 0;
    }
    
    • 1

    信息

    ID
    794
    时间
    1000ms
    内存
    256MiB
    难度
    4
    标签
    (无)
    递交数
    46
    已通过
    21
    上传者