题意:定义了一种表达式,让你判断这个式子是不是永真式。
因为命题变元最多只有5个,所以可以把写真值表的那个过程模拟一下,就ok了。
ps,这题居然1A,我本以为会wa很多次咧。。。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int q,p,r,s,t;
int pos;
int judge(char *str)
{
int t1,t2;
switch(str[pos])
{
case 'p':
pos++;
return p;
case 'q':
pos++;
return q;
case 'r':
pos++;
return r;
case 's':
pos++;
return s;
case 't':
pos++;
return t;
case 'K':
pos++;
t1=judge(str);
t2=judge(str);
return t1&t2;
case 'A':
pos++;
t1=judge(str);
t2=judge(str);
return t1|t2;
case 'N':
pos++;
t1=judge(str);
return 1-t1;
case 'C':
pos++;
t1=judge(str);
t2=judge(str);
return (1-t1)|t2;
case 'E':
pos++;
t1=judge(str);
t2=judge(str);
return t1 == t2;
}
}
int main()
{
char str[300];
while (1)
{
gets(str);
if (strcmp(str,"0") == 0)
break;
for (q=0; q<2; q++)
{
for (p=0; p<2; p++)
{
for (r=0; r<2; r++)
{
for (s=0; s<2; s++)
{
for (t=0; t<2; t++)
{
pos=0;
if (judge(str) == 0)
goto exit;
}
}
}
}
}
exit:
if (q != 2)
{
printf("not\n");
}
else
{
printf("tautology\n");
}
}
}
很无节操地用了goto。。。。