結果
| 問題 |
No.708 (+ー)の式
|
| コンテスト | |
| ユーザー |
vjudge1
|
| 提出日時 | 2025-11-16 11:04:30 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,059 bytes |
| コンパイル時間 | 1,741 ms |
| コンパイル使用メモリ | 169,796 KB |
| 実行使用メモリ | 7,848 KB |
| 最終ジャッジ日時 | 2025-11-16 11:04:33 |
| 合計ジャッジ時間 | 2,785 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | WA * 3 |
| other | WA * 12 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
string p;
stack<char> s; // ???
stack<char> op; // ??
/*????????*/
stack<int> num; // ????
stack<int> tmp; // ???????????
/*??????????*/
inline int id(char ch)
{ /*????????????????*/
if (ch == '+' || ch == '-')
return 1;
if (ch == '*' || ch == '/')
return 2;
if (ch == '^')
return 3;
if (ch == '(' || ch == ')')
return 0;
return -1;
}
inline string solve(string p)
{ /*?????*/
while (s.size())
s.pop();
while (op.size())
op.pop();
for (int i = 0; i < int(p.size()); i++)
{
if (p[i] >= '0' && p[i] <= '9')
s.push(p[i]);
else
{
if (p[i] == '(')
{
op.push(p[i]);
continue;
}
if (p[i] == ')')
{
while (!op.empty() && op.top() != '(')
{
s.push(op.top());
op.pop();
}
if (!op.empty())
op.pop();
continue;
}
if (id(p[i]) <= 2)
{
while (!op.empty() && id(op.top()) >= id(p[i]))
{
s.push(op.top());
op.pop();
}
}
else
{
while (!op.empty() && id(op.top()) > id(p[i]))
{
s.push(op.top());
op.pop();
}
}
op.push(p[i]);
}
}
while (!op.empty())
{
s.push(op.top());
op.pop();
}
string res = "";
while (!s.empty())
{
res += s.top();
s.pop();
}
reverse(res.begin(), res.end()); // ??
return res;
}
inline int f(int x, int y, char ch)
{
if (ch == '+')
return x + y;
if (ch == '-')
return x - y;
if (ch == '*')
return x * y;
if (ch == '/')
return x / y;
if (ch == '^')
return int(pow(x, y));
return -1;
}
inline void Query(string s)
{ /*?????????*/
while (num.size())
num.pop();
for (int i = 0; i < int(s.size()); i++)
{
if (s[i] >= '0' && s[i] <= '9')
num.push(s[i] - '0');
else
{
while (tmp.size())
tmp.pop();
while (num.size())
{
tmp.push(num.top());
num.pop();
}
while (tmp.size())
{
num.push(tmp.top());
cout << tmp.top() << " ";
tmp.pop();
}
// for(int j=i;j<int(s.size());j++)cout<<s[j]<<" ";
// cout<<"\n";
int a = num.top();
num.pop();
int b = num.top();
num.pop();
num.push(f(b, a, s[i]));
}
}
cout << num.top() << "\n";
}
signed main()
{
cin >> p;
Query(solve(p));
}
vjudge1