• Day1-A-POJ-3295


    由题意知,有5种操作,5个未知数,可0可1,一串操作问是否恒为1,最多100个字符,直接栈模拟所有情况即可

    代码如下:

    int p, q, r, s, t;
    
    bool calculate(string ind) {
        int length = ind.size();
        stack<int> buf;
        for (int i = length - 1; i >= 0;--i) {
            char tmp = ind[i];
            if(tmp == 'p')
                buf.push(p);
            else if(tmp == 'q')
                buf.push(q);
            else if(tmp == 'r')
                buf.push(r);
            else if(tmp == 's')
                buf.push(s);
            else if(tmp == 't')
                buf.push(t);
            else if(tmp == 'K') {
                int val1, val2;
                val1 = buf.top(), buf.pop();
                val2 = buf.top(), buf.pop();
                buf.push(val1 & val2);
            }
            else if(tmp == 'A') {
                int val1, val2;
                val1 = buf.top(), buf.pop();
                val2 = buf.top(), buf.pop();
                buf.push(val1 || val2);
            }
            else if(tmp == 'N') {
                int val = buf.top();
                buf.pop();
                buf.push(!val);
            }
            else if(tmp == 'C') {
                int val1, val2;
                val1 = buf.top(), buf.pop();
                val2 = buf.top(), buf.pop();
                buf.push(!val1 || val2);
            }
            else if(tmp == 'E') {
                int val1, val2;
                val1 = buf.top(), buf.pop();
                val2 = buf.top(), buf.pop();
                buf.push(val1 == val2);
            }
        }
    
        return buf.top();
    }
    
    int main() {
        string str;
        while(cin >> str && str != "0") {
            bool flag = true;
            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(!calculate(str)) {
                                    flag = false;
                                    break;
                                }
                            }
                            if(!flag)break;
                        }
                        if(!flag)break;
                    }
                    if(!flag)break;
                }
                if(!flag)break;
            }
            if(flag)
                printf("tautology
    ");
            else
                printf("not
    ");
            }
        return 0;
    }
     
  • 相关阅读:
    python之路--day22--多态....property..calssmethod....staticmethod
    python之路--day21--组合与封装
    内存数据库
    什么时候调用:拷贝构造函数、赋值运算符
    类成员初始化顺序
    HTTP、TCP、UDP的区别
    进程和线程的区别
    C++中类的静态成员与实例成员的区别
    设计模式
    const函数
  • 原文地址:https://www.cnblogs.com/GRedComeT/p/11212229.html
Copyright © 2020-2023  润新知