QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#371537 | #6750. Calculate | Kryc# | RE | 0ms | 3628kb | C++17 | 2.1kb | 2024-03-30 13:48:55 | 2024-03-30 13:49:00 |
Judging History
answer
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<deque>
#include<map>
#define x first
#define y second
#define ll long long
#define ULL unsigned long long
using namespace std;
typedef pair<int, int> PII;
const int N = 2e5 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;
stack<int> num;
stack<char> op;
void eval()
{
int a = num.top();
num.pop();
int b = num.top();
num.pop();
char c = op.top();
op.pop();
int x;
if(c == '-') x = b - a;
else if( c == '+') x = a + b;
num.push(x);
}
void solve()
{
string s;
cin >> s;
int len = s.length();
int ans = 0, f1 = 0, f2 = 0;
for(int i = 0; i < len; i ++ )
{
if(isdigit(s[i]))
{
if(f2)
{
if(op.top() == '-')
num.push(-(int)(s[i] - '0'));
else
num.push((int)(s[i] - '0'));
op.pop();
f2 = 0;
}
else
num.push((int)(s[i] - '0'));
}
else if(s[i] == '?')
{
f2 = 1;
if(f1)
{
if(op.top() == '-')
{
ans ++;
op.pop();
}
else if(op.top() == '+') op.pop();
}
else
{
if(i == 0) ans ++;
else if(op.size())
{
if(op.top() == '(') ans ++;
else if(op.top() == '+') ans ++, op.pop();
else if(op.top() == '-') op.pop();
}
}
}
else if(s[i] == '(')
{
if(op.size() && op.top() == '-') f1 = 1;
op.push(s[i]);
}
else if(s[i] == ')')
{
while(op.top() != '(' && op.size()) eval();
op.pop();
f1 = 0;
}
else
{
while(op.size() && op.top() != '(') eval();//&& num.size() > 1
op.push(s[i]);
}
}
while(op.size()) eval();
/*
if(num.size()) cout << num.top() << ' ';
else cout << 0 << ' ';
cout << ans << endl;
*/
int sum = 0;
if(num.size()) sum += num.top();
cout << sum + ans * 9 << endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
//cin >> t;
t = 1;
while(t -- )
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3580kb
input:
?+?
output:
18
result:
ok 1 number(s): "18"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3628kb
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+...