1 条题解

  • 0
    @ 2025-4-7 21:41:51

    C++ :

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #define ADD 1002
    #define MIN 1003
    #define MAX 1004
    int optrStack[301],ptr;
    
    int pop(){return optrStack[--ptr];}
    
    void push(int n){optrStack[ptr++]=n;}
    
    void readOptr(char *s,int &now){
        if(s[1]=='d')
            push(ADD);
        else if(s[1]=='i')
            push(MIN);
        else
            push(MAX);
        now+=4;
    }
    
    void readNum(char *s,int &now){
        int n,i=0;
        sscanf(s,"%d",&n);
        push(n);
        while(s[i]!=','&&s[i]!=')'){
            now++;
            i++;
        }
    }
    
    void operate(int &now){
        int a,b,opt;
        b=pop();
        a=pop();
        opt=pop();
        if(opt==ADD)
            push(a+b);
        else if(opt==MIN)
            push(a>b?b:a);
        else
            push(a<b?b:a);
        now++;
    }
    
    int main(){
        int n,len,i;
        char data[301];
        scanf("%d",&n);
        while(n--){
            scanf("%s",data);
            len=strlen(data);
            i=ptr=0;
            while(i<len){
                if(isalpha(data[i]))
                    readOptr(&data[i],i);
                else if(isalnum(data[i]))
                    readNum(&data[i],i);
                else if(data[i]==',')
                    i++;
                else
                    operate(i);
            }
            printf("%d\n",pop());
        }
    }
    
    • 1

    信息

    ID
    2011
    时间
    3000ms
    内存
    128MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者