简单题,不知道为什么想了很久。。。题意大概是,给一串由KANCE和pqrst构成的字符串,其中,K表&,A表|,N表!,C表(!x|y),E表!(x^y);
然后串中的p,q,r,s,t任取0或1,问逻辑值是否永远为正。
解题思路是:由于共有5个字母,最多可取32种不同的状态,把p看成二进制的第零位,依次到t,为二进制的第四位,枚举从1到32每个位的状态,然后分析字符串进行计算
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <stack>
using namespace std;
//看了好久才弄懂了题意
//第一次使用栈,本来不想用位运算,但想了想还是熟悉一下好
bool hash[205];
char WWF[105];
stack<bool>s;
void ope(char a)
{
if(a>='p'&&a<='t')
{
s.push(hash[a]);
return;
}
else if(a=='N')
{
bool x=s.top();
s.pop();
s.push(!x);
return;
}
bool x=s.top();
s.pop();
bool y=s.top();
s.pop();
if(a=='K')s.push(x&y);
if(a=='A')s.push(x|y);
if(a=='C')s.push(!x | y);
if(a=='E')s.push(!(x^y));
return;
}
int main()
{
while(cin>>WWF)
{
if(WWF[0]=='0')break;
int i;
for(i=0;i<(1<<5);i++)
{
hash['p']=i&1;
hash['q']=i&(1<<1);
hash['r']=i&(1<<2);
hash['s']=i&(1<<3);
hash['t']=i&(1<<4);
int len=strlen(WWF);
for(int j=len-1;j>=0;j--)
{
ope(WWF[j]);
}
bool ans=s.top();
s.pop();
if(ans==false)
{
cout<<"not"<<endl;
break;
}
}
if(i>=(1<<5))cout<<"tautology"<<endl;
}
return 0;
}
344

被折叠的 条评论
为什么被折叠?



