2 条题解
-
1康立扬 LV 7 @ 2022-4-24 9:51:49
python 3:
def calculate(s: str) -> int: def helper(s: list) -> int: stack = [] sign = '+' num = 0 while len(s) > 0: c = s.pop(0) if c.isdigit(): num = 10 * num + int(c) if c == '(': num = helper(s) if (not c.isdigit() and c != ' ') or len(s) == 0: if sign == '+': stack.append(num) elif sign == '-': stack.append(-num) elif sign == '*': stack[-1] = stack[-1] * num elif sign == '/': stack[-1] = int(stack[-1] / float(num)) elif sign == '^': stack[-1] = int(stack[-1]**num) num = 0 sign = c if c == ')': break return sum(stack) return helper(list(s)) a=input() print(calculate(a))
-
02024-12-8 20:21:41@
//半成品别复制 只是单纯懒得再写一次了 #include<bits/stdc++.h> using namespace std; const int N = 1e4 + 10; string a; stack<long long> st; long long sum; void calc(char ob) { long long a = st.top(); st.pop(); long long b = st.top(); st.pop(); if(ob == '+') st.push(a + b); else if(ob == '-') st.push(a - b); else if(ob == '*') st.push(a * b); else if(ob == '/') st.push(a / b); } int main(){ getline(cin,a); int len = a.size(); for(int i = 0;i < len;i++) { if(a[i] == '@') break; if(a[i] >= '0' && a[i] <= '9') sum = sum * 10 + a[i] - '0'; else if(a[i] == ' ') { st.push(sum); sum = 0; } else { calc(a[i]); } } cout << st.top(); return 0; }
- 1
信息
- ID
- 62
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 6
- 标签
- 递交数
- 102
- 已通过
- 29
- 上传者