P8035题解
题意
给你一串狮子,让你先带值,再求值。(类似于表达式求值)
思路
与表达式求值一样,直接用一个
注意
- 开
\text{double} - 精度
代码
#include<bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n; stack < double > st; double R[15]; string c; double sum() { double h = st.top(); st.pop(); if(st.top() == - 4) while(st.top() != - 1){st.pop(); h += st.top(); st.pop();} else { h = 1.0 / h; while(st.top() != -1){st.pop(); h += 1.0 / st.top(); st.pop();} h = 1.0 / h; } st.pop(); return h; } signed main() { scanf("%d", &n); for(int i = 1; i <= n; i++)scanf("%lf", &R[i]); cin >> c; for(int i = 0; i < c.size(); i++) { if(c[i] == '(')st.push(- 1); if(c[i] == ')')st.push(sum()); if(c[i] == '|')st.push(- 3); if(c[i] == '-')st.push(- 4); if(c[i] >= '0' and c[i] <= '9')st.push(R[c[i] - '0']); } printf("%.8lf", st.top()); return 0; }