QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#413596 | #6750. Calculate | Arnold_6 | RE | 0ms | 3696kb | C++14 | 2.5kb | 2024-05-17 19:40:38 | 2024-05-17 19:40:39 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
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(int(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();
}
int 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: 0ms
memory: 3556kb
input:
?+?
output:
18
result:
ok 1 number(s): "18"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3696kb
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+...