POJ3295

来源:互联网 发布:java程序下载 编辑:程序博客网 时间:2024/06/05 22:30

题意:
p,q,r,s,t分别代表0和1,K,A,N,C,E分别代表一种运算,具体的情况如下:K是x&&y;A是x||y;N是!x;C是(!x)||y;E是x==y;然后题目意思就是给你一连串的字符,
让你去判断它是否是永真式,如果是输出tautology,否则输出not
分析:
一开始的话是题目意思看了半天,就是那个C不知道是什么意思.明白了题目的意思后就比较好想思路,思路我的想法就是用栈来解决问题.具体就是把字符串从
最后面开始往栈里面添加,遇到是大写字母的就进行运算.就看最后的结果是1还是0.但是我不知道怎么来控制小写字母的0和1.于是乎,我的代码不知道怎么写.最后就在网上看别人的代码.看明白了后就自己写完了. 别人解决问题的方式真心是帅阿,用数组来当作栈使用,还有在控制小写字母的1和0的时候,直接用五个for循环搞定了,真的很佩服这样的思路,我只能是学习了 以后记住这样的写法,慢慢自己也搞出一写好的代码.而且据说还有用位运算来解决的,我也再去研究研究,学无止境.stay hungry stay foolish!
代码:

#include <iostream>#include <cmath>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;const int MAXX=110;//string str;int p,q,r,s,t;int stra[MAXX];//用来表示堆栈char str[MAXX];void First(){    int cns=0;    int len = strlen(str);    int t1=0,t2=0;    for(int i=len-1;i>=0;i--)    {        if(str[i]=='p')             stra[cns++]=p;        else if(str[i]=='q')             stra[cns++]=q;        else if(str[i]=='r')             stra[cns++]=r;        else if(str[i]=='s')             stra[cns++]=s;        else if(str[i]=='t')             stra[cns++]=t;        else if(str[i]=='K')        {            t1=stra[--cns];            t2=stra[--cns];            stra[cns++]=(t1&&t2);        }        else if(str[i]=='A')        {            t1=stra[--cns];            t2=stra[--cns];            stra[cns++]=(t1||t2);        }        else if(str[i]=='N')        {            t1=stra[--cns];            stra[cns++]=(!t1);        }        else if(str[i]=='C')        {            t1=stra[--cns];            t2=stra[--cns];            stra[cns++]=((!t1)||t2);        }        else if(str[i]=='E')        {            t1=stra[--cns];            t2=stra[--cns];            stra[cns++]=(t1==t2);        }    }}bool salve(){    for(p=0;p<2;p++)        for(q=0;q<2;q++)            for(r=0;r<2;r++)                for(s=0;s<2;s++)                    for(t=0;t<2;t++)                    {                        First();                        if(stra[0]==0)                            return false;                    }    return true;}int main(){    while(scanf("%s",&str))    {        if(strcmp(str,"0")==0)            break;        if(salve())            cout<<"tautology"<<endl;        else            cout<<"not"<<endl;    }    return 0;}
原创粉丝点击