zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    echarts在ie浏览器y轴坐标偏移向上
    git常用命令总结[转]
    char和varchar类型的区别
    关于猴子选大王的问题
    VIM折叠代码命令
    用excel打开文本内容
    php事务操作示例
    Iframe内嵌Cookie丢失问题
    VIM折叠代码命令
    git常用命令总结[转]
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/5725906.html
Copyright © 2011-2022 走看看