POJ-3295 Tautology (构造法)
POJ-3295 Tautology (构造法)#
题目链接poj-3295
大意就是pqrst五个布尔变量,KCEAN五个方法
K是并,A是或,N是取反,C是蕴含,E是相等
构造的是这个变量的2^5=32种取法很有意思
int a[35][6];
for (int i = 0; i < 32; i++)
{
a[i + 1][5] = i % 2;
a[i + 1][4] = i / 2 % 2;
a[i + 1][3] = i / 4 % 2;
a[i + 1][2] = i / 8 % 2;
a[i + 1][1] = i / 16 % 2;
}
结果是这样的
0 0 0 0 0
0 0 0 0 1
0 0 0 1 0
0 0 0 1 1
0 0 1 0 0
0 0 1 0 1
0 0 1 1 0
0 0 1 1 1
0 1 0 0 0
0 1 0 0 1
0 1 0 1 0
0 1 0 1 1
0 1 1 0 0
0 1 1 0 1
0 1 1 1 0
0 1 1 1 1
1 0 0 0 0
1 0 0 0 1
1 0 0 1 0
1 0 0 1 1
1 0 1 0 0
1 0 1 0 1
1 0 1 1 0
1 0 1 1 1
1 1 0 0 0
1 1 0 0 1
1 1 0 1 0
1 1 0 1 1
1 1 1 0 0
1 1 1 0 1
1 1 1 1 0
1 1 1 1 1
后面的算式用后缀表达式去模拟
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<stack>
using namespace std;
string str;
int main()
{
int a[35][6];
for (int i = 0; i < 32; i++)
{
a[i + 1][5] = i % 2;
a[i + 1][4] = i / 2 % 2;
a[i + 1][3] = i / 4 % 2;
a[i + 1][2] = i / 8 % 2;
a[i + 1][1] = i / 16 % 2;
}
for (int i = 1; i <= 32; i++)
{
for (int j = 1; j <= 5; j++)
cout << a[i][j] << " ";
cout << endl;
}
while (cin >> str)
{
if (str == "0")
break;
bool flag = false;
for (int i = 1; i < 33; i++)
{
stack<int>s;
for (int j = str.size() - 1; j >= 0; j--) {
if (str[j] >= 'p' && str[j] <= 't')
{
s.push(a[i][str[j] - 'o']);
}
else {
int n1, n2;
char now = str[j];
if (now == 'K')
{
n1 = s.top();
s.pop();
n2 = s.top();
s.pop();
s.push(n1 && n2);
}
if (now == 'A')
{
n1 = s.top();
s.pop();
n2 = s.top();
s.pop();
s.push(n1 || n2);
}
if (now == 'C')
{
n1 = s.top();
s.pop();
n2 = s.top();
s.pop();
s.push((!n1) || n2);
}
if (now == 'E')
{
n1 = s.top();
s.pop();
n2 = s.top();
s.pop();
s.push(n1 == n2);
}
if (now == 'N')
{
n1 = s.top();
s.pop();
s.push(!n1);
}
}
}
flag = s.top();
if (flag == 0)
{
cout << "not\n";
break;
}
}
if (flag)
cout << "tautology\n";
}
return 0;
}

浙公网安备 33010602011771号