Tautology--POJ 3295
生活随笔
收集整理的這篇文章主要介紹了
Tautology--POJ 3295
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、題目類型:模擬題、棧、位運算。
2、解題思路:(1)題意只用到5個 bool 變量,所以用0—32的 int 型用于循環(huán);(2)根據(jù)str的操作符性質(zhì)不停的出戰(zhàn)、入棧直到循環(huán)結(jié)束。(3)判斷最終棧中數(shù)據(jù)的真假,輸出結(jié)果。
3、注意事項:從str.length()-1開始循環(huán)出入棧;
4、實現(xiàn)方法:
#include<iostream>#include<string>
#include<stack>
using namespace std;
stack <bool> sta;
bool val[5],flag;
int main()
{
int i,j;
string str;
bool tmp1,tmp2;
while(cin>>str && str!="0")
{
flag=1;
for(i=0;i<32 && flag;i++)
{
memset(val,0,sizeof(val));
for(j=0;j<5;j++)
{
if(i & (1<<j))
val[j]=1;
}
for(j=str.length()-1;j>=0;j--)
{
switch(str[j])
{
case 'p':case 'q':case 'r':case 's':case 't':
sta.push(val[str[j]-'p']);
break;
case 'N':
tmp1=sta.top();
sta.pop();
sta.push(!tmp1);
break;
case 'E':
tmp1=sta.top();
sta.pop();
tmp2=sta.top();
sta.pop();
sta.push(tmp1==tmp2);
break;
case 'A':
tmp1=sta.top();
sta.pop();
tmp2=sta.top();
sta.pop();
sta.push(tmp1||tmp2);
break;
case 'K':
tmp1=sta.top();
sta.pop();
tmp2=sta.top();
sta.pop();
sta.push(tmp1 && tmp2);
break;
case 'C':
tmp1=sta.top();
sta.pop();
tmp2=sta.top();
sta.pop();
if(tmp2 && !tmp1)
tmp1=0;
else
tmp1=1;
sta.push(tmp1);
break;
default:
break;
};
}
if(!sta.top())
flag=0;
}
if(flag)
cout<<"tautology"<<endl;
else
cout<<"not"<<endl;
}
return 0;
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/yongze103/archive/2010/08/03/1791612.html
總結(jié)
以上是生活随笔為你收集整理的Tautology--POJ 3295的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第 2 章 常量、变量和表达式
- 下一篇: Nginx版本无缝升级