首页 > 其他 > 详细

poj 3295

时间:2016-01-11 13:46:27      阅读:127      评论:0      收藏:0      [点我收藏+]

题目意思就是计算表达式的值,如果所有情况下表达式为真就输出“tautology”,否则输出“not”。

p, q, r, s, and t,每个人有两种情况,综合起来一共有32种情况,枚举所有情况最后所有情况是真的话就是真。

K, A, N, C,E分别代表的是计算方式,e.iK代表逻辑语&,A代表逻辑语|,剩下的可以自己推了;

思路:先做一个预处理将所有情况都存到一个数组里面,然后就每一个情况分别枚举,处理方式的话是从表达式的最后面向前处理;

   因为是一个表达式,所以最后得到的结果只有一个数值,然后判断它是否为真就行了(注意一下:要将整个表达式的值在这一种情况下计算完才可以进行表达式真              假判断)

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int num[35][6];
char s[105];
int main()
{
    for(int i=0;i<32;++i)
        for(int j=0;j<5;++j)
            if(i&(1<<j))    num[i][j]=1;
            else            num[i][j]=0;
    while(scanf("%s",s)==1)
    {    
        if(s[0]==0)    break; 
        int flag=1;
        for(int k=0;k<32;++k){
            int len=strlen(s);
            int top=0;int goal[105];
            for(int j,i=len-1;i>=0;--i){
                if(s[i]>=p&&s[i]<=t)    goal[top++]=num[k][s[i]-p];
                else if(s[i]==K){
                    goal[top-2]=goal[top-2]&&goal[top-1];--top;
                }
                else if(s[i]==A){
                    goal[top-2]=goal[top-2]||goal[top-1];--top;
                }
                else if(s[i]==N)    goal[top-1]=!goal[top-1];
                else if(s[i]==C){
                    goal[top-2]=!goal[top-2]||goal[top-1];--top;
                }
                else if(s[i]==E){
                    goal[top-2]=goal[top-2]==goal[top-1];--top;
                }
            
            }
            if(goal[0]==0)    flag=0;
            if(flag==0)    break;        
        }
        if(flag)    cout << "tautology\n";
        else        cout <<    "not\n";    
    }    
}

 

 

别人递归的方法,学习学习

#include<stdio.h>  
02.#include<stdlib.h>  
03.#include<string.h>  
04.int state[5];  
05.char s[205];  
06.int l=0;  
07.int ind()                      
08.{  
09.    char ch=s[l++];  
10.    printf("");  
11.      
12.    switch(ch)  
13.    {  
14.    case p:  
15.    case q:  
16.    case r:  
17.    case s:  
18.    case t:  
19.        return state[ch-p];  
20.        break;  
21.    case K:  
22.        return ind()&ind();         
23.        break;  
24.    case A:  
25.        return ind()|ind();  
26.        break;  
27.    case N:  
28.        return !ind();  
29.        break;  
30.    case C:  
31.        return !ind()|ind();  
32.        break;  
33.    case E:  
34.        return ind()==ind();  
35.        break;  
36.    }  
37.}  
38.  
39.int main()  
40.{  
41.    scanf("%s", s);  
42.    while(s[0]!=0)  
43.    {  
44.        int len=strlen(s);  
45.        int mark=1;  
46.        for(state[0]=0; state[0]<=1 && mark; state[0]++)  
47.        {  
48.            for(state[1]=0; state[1]<=1 && mark; state[1]++)  
49.            {  
50.                for(state[2]=0; state[2]<=1 && mark; state[2]++)  
51.                {  
52.                    for(state[3]=0; state[3]<=1 && mark; state[3]++)  
53.                    {  
54.                        for(state[4]=0; state[4]<=1 && mark; state[4]++)  
55.                        {  
56.                l=0;            
57.                            if(ind()==0)  
58.                               mark=0;  
59.                        }  
60.                    }  
61.                 }  
62.            }  
63.        }  
64.        if(mark==1)  
65.            printf("tautology\n");  
66.        else  
67.            printf("not\n");  
68.        scanf("%s", s);  
69.    }  
70.    return 0;  
71.}   

挺巧妙的、

 

poj 3295

原文:http://www.cnblogs.com/sasuke-/p/5120742.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!