poj3295 http://poj.org/problem?id=3295
2012-10-23 12:03
288 查看
一道表达式求值的题目:
//思路: //5个变量(取0||1)有32种取值方式 可以将他们全部找出来 进行判断是不是为永真式 //从后面进行处理 //k->x&&y //A->x||y //N->!x //implies->(!x)||y //E->x==y; #include<iostream> #include<string.h> #include<stdio.h> #include<stack> using namespace std; stack<int> tem; string test; char map[6]; int p,q,r,s,t; int pp,qq,rr,ss,tt; int judge(char ch) { switch(ch) { case 'p': tem.push(pp); return 1; case 'q': tem.push(qq); return 1; case 'r': tem.push(rr); return 1; case 's': tem.push(ss); return 1; case 't': tem.push(tt); return 1; } return 0; } void res(char ch) { int a,b; switch(ch) { case 'K': a=tem.top(); tem.pop(); b=tem.top(); tem.pop(); tem.push(a&&b); break; case 'A': a=tem.top(); tem.pop(); b=tem.top(); tem.pop(); tem.push(a||b); break; case 'N': a=tem.top(); tem.pop(); tem.push(!a); break; case 'C': a=tem.top(); tem.pop(); b=tem.top(); tem.pop(); tem.push((!a)||b); break; case 'E': a=tem.top(); tem.pop(); b=tem.top(); tem.pop(); tem.push(a==b); break; } } int main() { int flag; while(getline(cin,test)) { if(test[0]=='0') break; flag=1; p=q=r=s=t=1; for(int i=0;i<5;i++) map[i]=0; for(int i=0; i<test.size(); i++) { if(test[i]=='p'&&p) { map[0]=1; p=0; } else if(test[i]=='q'&&q) { map[1]=1; q=0; } else if(test[i]=='r'&&r) { map[2]=1; r=0; } else if(test[i]=='s'&&s) { map[3]=1; s=0; } else if(test[i]=='t'&&t) { map[4]=1; t=0; } } // printf("%d %d %d %d\n",q,r,s,t); for( pp=p; pp<=1&&flag!=-1; pp++) for( qq=q; qq<=1&&flag!=-1; qq++) for( rr=r; rr<=1&&flag!=-1; rr++) for( ss=s; ss<=1&&flag!=-1; ss++) for( tt=t; tt<=1&&flag!=-1; tt++) { // printf("fsdf\n"); for(int i=test.size()-1; i>=0&&flag!=-1; i--) { if(!judge(test[i])) res(test[i]); } // printf("tem=%d\n",tem.top()); flag=tem.top()-1; // printf("flag=%d\n",flag); tem.pop(); } if(flag!=-1) printf("tautology\n"); else printf("not\n"); } return 0; }
相关文章推荐
- http://poj.org/problem?id=2186&&强连通分量
- http://poj.org/problem?id=2192
- POJ 1220 http://poj.org/problem?id=1220
- http://poj.org/problem?id=1287&&prim
- 青蛙的约会&&http://poj.org/problem?id=1061
- pku1882 Stamps(http://poj.org/problem?id=1882)(博客搬迁)
- http://poj.org/problem?id=2785
- http://poj.org/problem?id=1948..二维01背包。。
- http://poj.org/problem?id=3337
- http://poj.org/problem?id=1503
- http://poj.org/problem?id=3125
- 最小生成树 10.1.5.253 1505 poj 1258 http://poj.org/problem?id=1258
- (博客搬迁)pku2231 Moo Volume(数学题http://poj.org/problem?id=2231)
- http://poj.org/problem?id=3624
- POJ 1251(http://poj.org/problem?id=1251)
- http://poj.org/problem?id=2481 树状数组+优先队列
- http://poj.org/problem?id=2965
- http://poj.org/problem?id=2075
- http://poj.org/problem?id=1007
- Rails http://poj.org/problem?id=1363