题目大意
模拟电路的与、或、非运算,输出最后的结果。
解题思路
用栈模拟。
与、或运算需要两个运算数,则从栈里弹出两个数,运算结果压入栈。
非运算弹出一个数,处理后压入栈。
参考代码
#include<stdio.h>
#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
#include<climits>
#include<cmath>
#include<algorithm>
#include<queue>
#include<deque>
#include<map>
#include<unordered_map>
#include<set>
#include<stack>
//#define LOCAL //提交时一定注释
#define VI vector<int>
#define eps 1e-8
#define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long LL;
typedef double db;
const int inf = 0x3f3f3f3f;
const LL INF = 1e18;
const int N = 50;
#define ls rt << 1
#define rs rt << 1 | 1
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
inline int readint() {int x; scanf("%d", &x); return x;}
int an(int x, int y) { //与
return x && y;
}
int o(int x, int y) { //或
return x || y;
}
int no(int x) { //非
return x ^ 1;
}
int val[N];
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n = readint();
stack<int> st;
char c;
for(int i = 0; i < n; ++i) {
cin >> c;
val[i] = (c == 'T' ? 1 : 0);
}
while (cin >> c) {
if (isalpha(c)) {
st.push(val[c - 'A']);
}
else {
if (c == '*' || c == '+') {
int a = st.top(); st.pop();
int b = st.top(); st.pop();
// cout << "a:" << a << " " << "b:" << b;
int r = (c == '*' ? an(a, b) : o(a, b));
// cout << "now a:" << a << endl;
st.push(r);
}
else {
int a = st.top(); st.pop();
int r = no(a);
st.push(r);
}
}
}
cout << ((st.top()) ? 'T' : 'F');
return 0;
}