题目描述
WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of the possible symbols K, A, N, C, E, p, q, r, s, t. A Well-formed formula WFF) is any string of these symbols obeying the following rules:
p, q, r, s, and t are WFFs
if w is a WFF, Nw is a WFF
if w and x are WFFs, Kwx, Awx, Cwx, and Ewx are WFFs.
The meaning of a WFF is defined as follows:
p, q, r, s, and t are logical variables that may take on the value 0 false) or 1 true).
K, A, N, C, E mean and, or, not, implies, and equals as defined in the truth table below.
Definitions of K, A, N, C, and E
w x Kwx Awx Nw Cwx Ewx
1 1 1 1 0 1 1
1 0 0 1 0 0 0
0 1 0 1 1 1 0
0 0 0 0 1 1 1
A tautology is a WFF that has value 1 true) regardless of the values of its variables. For example, ApNp is a tautology because it is true regardless of the value of p. On the other hand, ApNq is not, because it has the value 0 for p=0, q=1.
You must determine whether or not a WFF is a tautology.
Input
Input consists of several test cases. Each test case is a single line containing a WFF with no more than 100 symbols. A line containing 0 follows the last case.
Output
For each test case, output a line containing tautology or not as appropriate.
Sample Input
ApNp
ApNq
0
Sample Output
tautology
not
题解:逻辑判断表达式,判断这个表达式是否为永真式。有5个数,所以共有32中情况,枚举+递归搞定。
代码如下:
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<math.h>
#define maxn 10000
#define INF 0x3f3f3f3f
using namespace std;
int q,p,r,s,t,pos;
char a[105];
bool cal)
{pos++;switcha[pos]){ //switch真的是好用啊,分情况递归即可case 'p': return p;case 'q': return q;case 'r': return r;case 's': return s;case 't': return t;case 'K': return cal)&cal);case 'A': return cal)|cal);case 'N': return !cal);case 'C': return !cal))|cal);case 'E': return cal)==cal);}
}
int main)
{whilescanf"%s",a)!=EOF && a[0] != '0'){bool flag = true;forp = 0; p < 2 && flag; p++) //这里的flag表示,只要有一种情况不成立,则推出循环forq = 0; q < 2 && flag; q++)forr = 0; r < 2 && flag; r++)fors = 0; s < 2 && flag; s++)fort = 0; t < 2 && flag; t++){pos = -1;flag = cal);}ifflag)printf"tautology\n");else printf"not\n");}return 0;
}
阅读世界,共赴山海423全民读书节,邀你共读