• Tautology---poj3295(枚举判断是否为永真式)


    题目链接:http://poj.org/problem?id=3295

    题意:判断是否是永真式,其中 p q r s t 表示逻辑变量其值为0或者1;

    枚举所有逻辑变量的值,然后判断是否出现false

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<string>
    #include<vector>
    #include<algorithm>
    #include<map>
    #include<queue>
    #include<stack>
    
    using namespace std;
    
    #define met(a, b) memset(a, b, sizeof(a))
    #define N 111
    
    typedef long long LL;
    
    int p, q, r, s, t, len;
    char str[N];
    
    bool Judge()
    {
        stack<int> Sta;
        for(int i=len-1; i>=0; i--)
        {
            int x1, x2;
            if(str[i] == 'p')Sta.push(p);
            else if(str[i] == 'q')Sta.push(q);
            else if(str[i] == 'r')Sta.push(r);
            else if(str[i] == 's')Sta.push(s);
            else if(str[i] == 't')Sta.push(t);
            else if(str[i] == 'K')
            {
                x1 = Sta.top();Sta.pop();
                x2 = Sta.top();Sta.pop();
                Sta.push(x1&&x2);
            }
            else if(str[i] == 'A')
            {
                x1 = Sta.top();Sta.pop();
                x2 = Sta.top();Sta.pop();
                Sta.push(x1||x2);
            }
            else if(str[i] == 'N')
            {
                x1 = Sta.top();Sta.pop();
                Sta.push(!x1);
            }
            else if(str[i] == 'C')
            {
                x1 = Sta.top();Sta.pop();
                x2 = Sta.top();Sta.pop();
                Sta.push( !(x1&&!x2) );
            }
            else
            {
                x1 = Sta.top();Sta.pop();
                x2 = Sta.top();Sta.pop();
                Sta.push( x1==x2 );
            }
        }
        return Sta.top()==1;
    }
    
    bool solve()
    {
        len = strlen(str);
    
        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++)
            if(!Judge())
                return false;
        return true;
    }
    
    int main()
    {
        while(scanf("%s", str), strcmp(str, "0"))
        {
            if(solve())puts("tautology");
            else puts("not");
        }
        return 0;
    }
    View Code
  • 相关阅读:
    用vbox搭建Linux服务器
    mysql数据库两表关联查询统计同一字段不同值的个数
    2019-06-16 Java学习日记之XML&tomcat
    2019-06-15 Java学习日记之mysql多表查询
    2019-06-14 Java学习日记之SQL
    2019-06-13 Java学习日记之MySql
    XML & Tomcat
    数据库的CRUD操作
    PrepareStatement
    Dao模式(data Access Object 数据访问对象)
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/5725906.html
Copyright © 2020-2023  润新知