POJ3295-Tautology

本文介绍了一种通过枚举变量所有可能值来判断逻辑表达式真假的方法,并提供了一个使用C++实现的具体示例。该程序能够处理包含五个变量(p, q, r, s, t)的复杂逻辑表达式,利用栈结构逆向解析表达式并计算其真值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:点击打开链接

题目大意是根据给出的运算法则,判断式子是否真,变量有5个,枚举所有可能值,其中有假就为假。

//164K	0MS	
//C++	1899B
#include <cstdio>
#include <cstring>
#define abs(i) (i>=0?i:-i)
int p,q,r,s,t;
char str[111];
int st[111];
void x()
{
    int top=0;
    int len=strlen(str);
    for(int i=len-1;i>=0;i--)
    {
        if(str[i]=='p') st[top++]=p;
        else if(str[i]=='q') st[top++]=q;
        else if(str[i]=='r') st[top++]=r;
        else if(str[i]=='s') st[top++]=s;
        else if(str[i]=='t') st[top++]=t;
        else if(str[i]=='K')
        {
            int t1=st[--top];
            int t2=st[--top];
            if(t1&&t2)
                st[top++]=1;
            else
                st[top++]=0;
        }
        else if(str[i]=='A')
        {
            int t1=st[--top];
            int t2=st[--top];
            if(t1||t2)
                st[top++]=1;
            else
                st[top++]=0;
        }
        else if(str[i]=='C')
        {
            int t1=st[--top];
            int t2=st[--top];
            if(t1==1&&t2==0)
                st[top++]=0;
            else
                st[top++]=1;
        }
        else if(str[i]=='E')
        {
            int t1=st[--top];
            int t2=st[--top];
            if(t1==t2)
                st[top++]=1;
            else
                st[top++]=0;
        }
        else if(str[i]=='N')
        {
            int t1=st[--top]-1;
            st[top++]=abs(t1);
        }
    }
}
bool f()
{
    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++)
                    {
                        x();
                        if(st[0]==0)
                            return false;
                    }
    return true;
}
int main()
{
    while(~scanf("%s",str)&&strcmp(str,"0"))
    {
        if(f())
        {
            printf("tautology\n");
        }
        else
        {
            printf("not\n");
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值