poj3295Tautology
2016-05-29 17:15
239 查看
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>
表达式运算符合栈的结构特点,构造一个栈进行运算。
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>
相关文章推荐
- GitHub Pages上使用Hexo框架建立静态博客
- qt5.3.2下载地址
- 【学习随笔】今日项目代码遇到的两个问题
- ListView高效异步加载图片
- ASP.NET Security
- java大数模板
- mongodb的安装
- 顺时针和逆时针螺旋打印二维数组(行列式)
- JAVA学习(14) 多线程-Runnable接口
- Isomorphic Strings
- CentOS,Ubuntu,Gentoo,Freebsd,RedHat,Debian的区别及选择
- 修改内核临时端口范围
- eclipse中遇到的快捷键问题
- C++ map 中的reverse_iterator
- 同步代码块什么时候释放锁
- MTSimpleAdapter框架扩展——添加empty页面、error页面和loading等特性
- 内外网同时上网的方法
- MAC OS下JDK版本切换指南
- MAC OS下JDK版本切换指南
- MAC OS下JDK版本切换指南