http://poj.org/problem?id=3295
题目大意:
数学题 判断表达式是否为永真式
p,q,r,s,t --- 变量
K --- 且
A --- 或
N --- 非
C --- 蕴含
E --- 等价
分析:
永真式 即对所有赋值情况 表达式都为真 枚举变量所有赋值 共 25 种情况 表达式真值的计算类似与前缀表达式
AC代码:
#include <stdio.h>
#include <string.h>
#include <stack>
#include <math.h>
using namespace std;
char str[500];
int p,q,r,s,t;
int ans(){
stack<char> S1;
stack<int> S2;
for (int i=0;i<strlen(str);i++){
S1.push(str[i]);
}
while (!S1.empty()){
char temp=S1.top();
S1.pop();
if(temp=='p'){
S2.push(p);
}
else if(temp=='q'){
S2.push(q);
}
else if(temp=='r'){
S2.push(r);
}
else if(temp=='s'){
S2.push(s);
}
else if(temp=='t'){
S2.push(t);
}
else if(temp=='N'){
int a=S2.top();
S2.pop();
S2.push(!a);
}
else if(temp=='A'){
int a=S2.top();
S2.pop();
int b=S2.top();
S2.pop();
S2.push(a||b);
}
else if(temp=='K'){
int a=S2.top();
S2.pop();
int b=S2.top();
S2.pop();
S2.push(a&&b);
}
else if(temp=='E'){
int a=S2.top();
S2.pop();
int b=S2.top();
S2.pop();
S2.push(!(a^b));
}
else if(temp=='C'){
int a=S2.top();
S2.pop();
int b=S2.top();
S2.pop();
if(b==1&&a==0){
S2.push(0);
}
else
S2.push(1);
}
}
return S2.top();
}
int oper(){
for (int i=0;i<=pow(2,5);i++){// 所有情况
int x=i;
p=x%2;
x/=2;
q=x%2;
x/=2;
r=x%2;
x/=2;
s=x%2;
x/=2;
t=x%2;
if(ans()==0)
return 0;
}
return 1;
}
int main (){
while (scanf ("%s",str)){
if(str[0]=='0')
break;
if(oper())
printf ("tautology\n");
else
printf ("not\n");
}
return 0;
}