poj3295Tautology

来源:互联网 发布:网络吞吐量和带宽 编辑:程序博客网 时间:2024/05/16 11:24

http://poj.org/problem?id=3295

表达式运算符合栈的结构特点,构造一个栈进行运算。

p,q,r,s,t的值共有2的五次方种可能组合,当成二进制不断加一进行枚举。

<span style="font-size:18px;">#include <stdio.h>#include <string.h>char wff[200];int operand[200],value[6];int Caculate(char ch,int u,int v){    switch(ch)    {        case'K':return u&&v;        case'A':return u||v;        case'C':return !u||v;        case'E':return !(u^v);    }}int main(){    int i,top,x,y,z,tag,c,n,len;    while(scanf("%s",wff)!=EOF)    {        if(wff[0] == '0')            break;        memset(value,0,sizeof(value));        n=0;        tag=1;        len=strlen(wff);        while(n<32)//枚举pqrst的所有情况 (二进制)        {            top=0;            memset(operand,0,sizeof(operand));            for(i=len-1;i>=0;i--)//逆向扫描表达式            {                if(wff[i]>='p'&&wff[i]<='t')                    operand[top++]=value[wff[i]-112];                else if(wff[i]=='N')                {                    z=operand[--top];                    operand[top++]=!z;                }                else if(wff[i]=='K'|| wff[i]=='A'|| wff[i]=='C'|| wff[i]=='E')                {                    x=operand[--top];                    y=operand[--top];                    operand[top++]=Caculate(wff[i],y,x);                }            }            if(operand[top-1]==0)//取出结果            {                tag=0;                break;//跳出枚举循环            }            value[0]++;            c=0;            while(value[c]>1)            {                value[c]=0;                c++;                value[c]++;            }            n++;        }        if(tag)            printf("tautology\n");        else            printf("not\n");    }    return 0;}</span>


0 0
原创粉丝点击