QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#413598#6750. CalculateArnold_6RE 1ms3596kbC++142.5kb2024-05-17 19:41:532024-05-17 19:41:54

Judging History

你现在查看的是最新测评结果

  • [2024-05-17 19:41:54]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3596kb
  • [2024-05-17 19:41:53]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
#define int long long
int cal(string s)
{
    int len=s.length();

    stack<char>op;
    string ex;
    ex.clear();
    for(int i=0;i<len;i++)
    {
        if(s[i]>='0' && s[i]<='9')ex.push_back(s[i]);
        else
        {
            if(op.empty())op.push(s[i]);
            else if(s[i]=='(')op.push(s[i]);
            else if(s[i]=='+'||s[i]=='-')
            {
                if(op.top()!='(')
                {
                    ex.push_back(op.top());
                    op.pop();
                    op.push(s[i]);
                }
                else
                {
                    op.push(s[i]);
                }
            }
            else if(s[i]==')')
            {
                ex.push_back(op.top());
                op.pop();
                op.pop();
            }
        }
        // cout<<ex<<endl;
    }
    while(op.size())ex.push_back(op.top()),op.pop();
    // cout<<ex<<endl;

    len=ex.size();
    stack<int>num;
    for(int i=0;i<len;i++)
    {
        if(ex[i]>='0'&&ex[i]<='9')
        {
            num.push((ex[i]-'0'));
        }
        else
        {
            int b=num.top();
            num.pop();
            int a=num.top();
            num.pop();
            int t;
            if(ex[i]=='+')t=a+b;
            else t=a-b;
            num.push(t);
        }
    }
    // cout<<num.top()<<endl;
    return num.top();
}

signed main()
{
    string s;
    cin>>s;
    int len=s.length();
    int pos[3],cnt=0;
    for(int i=0;i<len;i++)
    {
        if(s[i]=='?')
        {
            cnt++;
            pos[cnt]=i;
        }
    }
    if(cnt==0)
    {
        cout<<cal(s);
    }
    if(cnt==1)
    {
        string st;
        st=s;
        st[pos[1]]='9';
        int ans=cal(st);
        st[pos[1]]='0';
        ans=max(ans,cal(st));

        // cout<<st<<endl;
        cout<<ans;
    }
    if(cnt==2)
    {
        string st;
        st=s;
        st[pos[1]]='0';
        st[pos[2]]='0';
        // cout<<st<<endl;
        int ans=cal(st);
        

        st[pos[1]]='0';
        st[pos[2]]='9';
        // cout<<st<<endl;
        ans=max(ans,cal(st));
        

        st[pos[1]]='9';
        st[pos[2]]='0';
        // cout<<st<<endl;
        ans=max(ans,cal(st));
        
        
        st[pos[1]]='9';
        st[pos[2]]='9';
        // cout<<st<<endl;
        ans=max(ans,cal(st));
        cout<<ans;
    }
    // system("pause");
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3576kb

input:

?+?

output:

18

result:

ok 1 number(s): "18"

Test #2:

score: 0
Accepted
time: 1ms
memory: 3596kb

input:

(?+9)-(?+1)

output:

17

result:

ok 1 number(s): "17"

Test #3:

score: -100
Runtime Error

input:

((9)-(((8)-(2))+(((1+(1))-(1+((2)+2+2)))+(5)+4))+(((7)-((9)+3))-((8)-(0-(2))+0))+((6)-(6+(((4)-(9))-(8-((9)+(1))+(0)))+(2-((9)+7))-(1)))-((((7)+(1))-((3)+(3)))-((2)-((6)-((3)-(8)))))+(2+0-((6)-(1))))-((((3)-(((0)+((4)-(9))+((6+8)+4)+(5)-(4-(3)-(8)))-((8)-(2))))+(((2)-(4))+(6)-(2))+(6-(1))-((2+9)-(3+...

output:


result: