QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#371576 | #6750. Calculate | Kryc# | WA | 1ms | 3812kb | C++17 | 2.1kb | 2024-03-30 14:06:52 | 2024-03-30 14:06:54 |
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();
num.push(0);
f1 = 0;
}
else
{
while(op.size() && op.top() != '(') eval();//&& num.size() > 1
op.push(s[i]);
}
}
num.push(0);
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: 0
Wrong Answer
time: 1ms
memory: 3812kb
input:
?+?
output:
0 2
result:
wrong answer 1st numbers differ - expected: '18', found: '0'