传送门:QAQ
题意:给你一个表达式,然后给你几张表,让你枚举有多少种赋值方法能使答案为2.
思路:模拟一下中缀表达式转成后缀表达式,暴力枚举就行了。
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
#define LL long long int
char ch[100];
int nummm;
stack<char>mu;
stack<int>fin;
int jian[10] = { 2,1,0 };
int jia[5][5][5] = { { {0,0,0},{0,1,1},{0,1,2,} } ,{ { 0,1,2 },{ 1,1,2 },{ 2,2,2, } } };
int main(void) {
while (gets(ch)) {
if (strlen(ch) == 1 && ch[0] == '.') {
break;
}
int tot = 0;
int len = strlen(ch);
for (int p = 0; p < 3; p++) {
for (int q = 0; q < 3; q++) {
for (int r = 0; r < 3; r++) {
while (!fin.empty()) {
fin.pop();
}
while (!mu.empty()) {
mu.pop();
}
for (int i = 0; i < len; i++) {
if (ch[i] == ' ')
continue;
if (ch[i] == 'P' ) {
fin.push(p);
while(!mu.empty() && mu.top()=='-') {
mu.pop();
int pp = fin.top();
fin.pop();
fin.push(jian[pp]);
}
}
else if (ch[i] == 'Q') {
fin.push(q);
while (!mu.empty() && mu.top() == '-') {
mu.pop();
int qq = fin.top();
fin.pop();
fin.push(jian[qq]);
}
}
else if (ch[i] == 'R') {
fin.push(r);
while (!mu.empty() && mu.top() == '-') {
mu.pop();
int rr = fin.top();
//printf("%d\n", rr);
fin.pop();
fin.push(jian[rr]);
//printf("%d\n", jian[rr]);
}
}
else if (ch[i] >= '0'&&ch[i] <= '2') {
fin.push(ch[i] - '0');
while (!mu.empty()&&mu.top() == '-') {
mu.pop();
int gg = fin.top();
fin.pop();
fin.push(jian[gg]);
}
}
else {
if (ch[i] == '-') {
mu.push(ch[i]);
}
else if (ch[i] == '(') {
mu.push(ch[i]);
}
else if (ch[i] == '+') {
if (!mu.empty() && mu.top() == '(') {
mu.push('1');
}
else if (mu.size() == 0) {
mu.push('1');
}
else {
int a1 = fin.top();
fin.pop();
int a2 = fin.top();
fin.pop();
fin.push(jia[mu.top() - '0'][a1][a2]);
mu.pop();
}
}
else if (ch[i] == '*') {
if (!mu.empty() && mu.top() == '(') {
mu.push('0');
}
else if (mu.size() == 0) {
mu.push('0');
}
else {
int a1 = fin.top();
fin.pop();
int a2 = fin.top();
fin.pop();
fin.push(jia[mu.top() - '0'][a1][a2]);
mu.pop();
}
}
else if (ch[i] == ')') {
while (mu.top()!= '(') {
int a1 = fin.top();
fin.pop();
int a2 = fin.top();
fin.pop();
fin.push(jia[mu.top() - '0'][a1][a2]);
//printf("%d\n", jia[mu.top() - '0'][a1][a2]);
mu.pop();
}
mu.pop();
while (!mu.empty() && mu.top() == '-') {
mu.pop();
int pp = fin.top();
fin.pop();
fin.push(jian[pp]);
}
}
}
}
if (mu.size() >= 1&&fin.size()==2) {
int a1 = fin.top();
fin.pop();
int a2 = fin.top();
fin.pop();
fin.push(jia[mu.top() - '0'][a1][a2]);
mu.pop();
while (!mu.empty() && mu.top() == '-') {
mu.pop();
int pp = fin.top();
fin.pop();
fin.push(jian[pp]);
}
}
if (fin.size() == 1 && fin.top() == 2) tot++;
}
}
}
printf("%d\n", tot);
}
return 0;
}