题意:判断一个字符串是否是用真式。
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 |
题解:枚举 p,q,r,s,t 的布尔值,用一个整数表示。
#include <iostream>
using namespace std;
int Nth_Char;
char str[101];
bool step ( char str[101], int logicValue )
{
Nth_Char++;
switch ( str[Nth_Char] )
{
case 'p': return logicValue & 1;
case 'q': return ( logicValue >> 1 ) & 1;
case 'r': return ( logicValue >> 2 ) & 1;
case 's': return ( logicValue >> 3 ) & 1;
case 't': return ( logicValue >> 4 ) & 1;
case 'N': return !step ( str, logicValue );
case 'K': return step ( str, logicValue ) & step ( str, logicValue );
case 'A': return step ( str, logicValue ) | step ( str, logicValue );
case 'C': return !step (str, logicValue ) | step ( str, logicValue );
case 'E': return step ( str, logicValue ) == step ( str, logicValue );
default:;
}
}
bool judge ( char str[101] )
{
for ( int i = 0; i < 32; i++ ) /* 枚举p,q,r,s,t的布尔值 */
{
Nth_Char = -1;
if ( !step ( str, i ) )
return false;
}
return true;
}
int main()
{
while ( cin >> str && str[0] != '0' )
{
if ( judge ( str ) )
cout << "tautology" << endl;
else
cout << "not" << endl;
}
return 0;PO
}