POJ3295

题意:
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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值