zoukankan      html  css  js  c++  java
  • Tautology(递推)||(栈(stack))(待整理)

    Description

    WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of the possible symbols K, A, N, C, E, p, q, r, s, t. A Well-formed formula (WFF) is any string of these symbols obeying the following rules:

    • p, q, r, s, and t are WFFs
    • if w is a WFF, Nw is a WFF
    • if w and x are WFFs, Kwx, Awx, Cwx, and Ewx are WFFs.
    The meaning of a WFF is defined as follows:
    • p, q, r, s, and t are logical variables that may take on the value 0 (false) or 1 (true).
    • K, A, N, C, E mean and, or, not, implies, and equals as defined in the truth table below.
    Definitions of K, A, N, C, and E
         w  x   Kwx   Awx    Nw   Cwx   Ewx
      1  1   1   1    0   1   1
      1  0   0   1    0   0   0
      0  1   0   1    1   1   0
      0  0   0   0    1   1   1

    A tautology is a WFF that has value 1 (true) regardless of the values of its variables. For example,ApNp is a tautology because it is true regardless of the value of p. On the other hand,ApNq is not, because it has the value 0 for p=0, q=1.

    You must determine whether or not a WFF is a tautology.

    Input

    Input consists of several test cases. Each test case is a single line containing a WFF with no more than 100 symbols. A line containing 0 follows the last case.

    Output

    For each test case, output a line containing tautology or not as appropriate.

    Sample Input

    ApNp
    ApNq
    0

    Sample Output

    tautology
    not

    题意:

    题意很简单,就是数学中的运算符运算;

    第一种解法是运用了栈的思想,这样的话比较简单

    #include<iostream>
    #include<stdio.h>
    #include<cstring>
    #include<stack>
    using namespace std;
    stack <bool> st;
    int p,q,r,s,t;
    
    
    void push(char c){//对于运算符压入栈顶
        if(c=='p') st.push(p);
        else if(c=='q') st.push(q);
        else if(c=='r') st.push(r);
        else if(c=='s') st.push(s);
        else if(c=='t') st.push(t);
        return;
    }
    void f(char c){//对经判断之后的操作符进行运算函数
        bool a,b;
        if(c=='K'){
            a=st.top();
            st.pop();
            b=st.top();
            st.pop();
            st.push(a&&b);
        }
        else if(c=='A'){
            a=st.top();
            st.pop();
            b=st.top();
            st.pop();
            st.push(a||b);
        }
        else if(c=='N'){
            a=st.top();
            st.pop();
            st.push(!a);
        }
        else if(c=='C'){
            a=st.top();
            st.pop();
            b=st.top();
            st.pop();
            st.push((!a)||b);
        }
        else if(c=='E'){
            a=st.top();
            st.pop();
            b=st.top();
            st.pop();
            st.push(a==b);
        }
        return;
    }
    int main(){
        char wff[110];
        while(scanf("%s",wff)&&wff[0]!='0'){
            int len=strlen(wff);
            bool flag=1;
            int i;
            for(p=0;p<=1;p++)
            {
                for(q=0;q<=1;q++)
                     for(r=0;r<=1;r++)
                        for(s=0;s<=1;s++)
                            for(t=0;t<=1;t++)
                            {
                                for(i=len-1;i>=0;i--)
                                {
                                    if(c=='p'||c=='q'||c=='r'||c=='s'||c=='t')
                                        push(wff[i]);//将当前字符压入栈
                                    else
                                        f(wff[i]);//对栈顶进行操作,进行运算
                                }
                                if(st.top()==0)//此时的top是最后的结果
                                    flag=0;
                                st.pop();//将最后的结果清空,以便下一种情况的遍历
                            }
            }
            if(flag)
                printf("tautology
    ");
            else
                printf("not
    ");
        }
    }
    


     

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    int state[5];
    char s[205];
    int l=0;
    int ind(){
    	char ch=s[l++];
    	//printf("");
    
    	switch(ch){
    	case 'p':
    	case 'q':
    	case 'r':
    	case 's':
    	case 't':	return state[ch-'p'];
    
    	case 'K':	return ind()&ind();
    	case 'A':	return ind()|ind();
    	case 'N':	return !ind();
    	case 'C':	return !ind()|ind();
    	case 'E':	return ind()==ind();
    	}
    }
    
    int main(){
        scanf("%s", s);
        while(s[0]!='0'){
            int len=strlen(s);
            int mark=1;
            for(state[0]=0; state[0]<=1 && mark; state[0]++){
                for(state[1]=0; state[1]<=1 && mark; state[1]++){
                    for(state[2]=0; state[2]<=1 && mark; state[2]++){
                        for(state[3]=0; state[3]<=1 && mark; state[3]++){
                            for(state[4]=0; state[4]<=1 && mark; state[4]++){
    							l=0;
                                if(ind()==0)
                                   mark=0;
                            }
                        }
                     }
                }
            }
            if(mark==1)
                printf("tautology
    ");
            else
                printf("not
    ");
            scanf("%s", s);
        }
        return 0;
    }
    


     

  • 相关阅读:
    火狐flash插件
    centos 安装php ide (eclipse + php 插件)
    编译器的工作过程
    php中调用mysql的存储过程和存储函数
    mysql 高性能
    存储过程/游标/mysql 函数
    php 生成二维码
    frameset,frame应用,常用于后台
    html5 meta头部设置
    CAReplicatorLayer复制Layer和动画, 实现神奇的效果
  • 原文地址:https://www.cnblogs.com/zswbky/p/5432078.html
Copyright © 2011-2022 走看看