POJ3295——Tautology
来源:互联网 发布:java环境配置 编辑:程序博客网 时间:2024/06/05 02:45
POJ3295
/************************************************************算法原理:WFF求值,从表达式的末尾向前读取字符,并分类出入栈。因为输入是合法的,最后栈顶(且只有一个)的元素即为表达式的逻辑值Notice:1)位&运算符的运用,可以精简代码2)判断K, A, N, C, E的逻辑值可以用已知二维数组3)还可以用递归求解************************************************************/#include<iostream>#include<stack>using namespace std;const int Length = 1000;stack<int> answers;char experssion[Length];int value[5];//在共2^5(32)种情况下判断void operaterStack(int pos)//根据expression进行出入栈的操作{if (experssion[pos] == 'N'){int a = answers.top(); answers.pop();answers.push(!a);}else if (experssion[pos] >= 'p'&&experssion[pos] <= 't')answers.push(value[experssion[pos] - 'p']);//value[0]-value[4]在32个循环里赋予了不同的值else{int a = answers.top(); answers.pop();int b = answers.top(); answers.pop();switch (experssion[pos]){case 'K':answers.push(a&&b); break;//或者按题目中给的bool值,用已知数组判断逻辑关系case 'A':answers.push(a || b); break;case 'C':answers.push(!a|| b); break;case 'E':answers.push(a == b); break;default:break;}}}int main(){while (cin >> experssion&&(experssion != "0"))//表达式结束的标志位'0'{int j;for (j = 0; j < (1 << 5); j++)//共32种情况{//详细见图for (int k = 0; k < 5; k++)value[k] = j & (1 << k);//类似于x&1判断奇偶int i = strlen(experssion);//while循环的条件有i--防止越界while (i--)operaterStack(i);//value[0]-value[4]在这个函数里发挥作用啦if (!answers.top()){cout << "not\n";//栈顶元素即为表达式最后的值answers.pop();//清空栈,给下一次循环使用break;}}if (j >= (1 << 5))//如果j是正常退出,即说明表达式值为1cout << "tautology\n";//此时j>=32}}/*非常酷的用递归的写法,在32个循环中判断即可int getval(){int temp1, temp2;switch (experssion[pos++]){case 'p': return (value & (1 << 0)) ? 1 : 0;case 'q': return (value & (1 << 1)) ? 1 : 0;case 'r': return (value & (1 << 2)) ? 1 : 0;case 's': return (value & (1 << 3)) ? 1 : 0;case 't': return (value & (1 << 4)) ? 1 : 0;case 'K': temp1 = getval(); temp2 = getval(); return temp1 & temp2;case 'A': temp1 = getval(); temp2 = getval(); return temp1 | temp2;case 'N': return !getval();case 'C': temp1 = !getval(); temp2 = getval(); return temp1 | temp2;case 'E': temp1 = getval(); temp2 = getval(); return temp1 == temp2;} }*/
0 0
- POJ3295——Tautology
- poj3295——Tautology(构造法)
- poj3295 Tautology —— 构造法
- POJ3295--Tautology
- poj3295-Tautology
- POJ3295 Tautology
- poj3295 Tautology
- poj3295--Tautology
- POJ3295-Tautology
- POJ3295-Tautology
- Poj3295 Tautology
- POJ3295 Tautology
- POJ3295-Tautology
- POJ3295 Tautology
- Tautology poj3295
- POJ3295 Tautology
- POJ3295 tautology 解题报告
- POJ3295 Tautology (遞歸+構造)
- nyoj99(欧拉路)
- OAF学习笔记-不基于EO的数据处理
- C 标准库 strstr 函数的实现
- Idea 15 64位安装
- LIGHTOJ-1043 -Triangle Partitioning
- POJ3295——Tautology
- I/O流性能比拼
- HDU 4325 Flowers(线段树+离散化)
- Android - User Interface
- 【OpenCV】边缘检测:梯度,sobel算子的理解
- ASCII码对应表chr(9)、chr(10)、chr(13)、chr(32)、chr(34)、chr(39)、
- Introduction to Recommender System 之 Module 4 User-User Collaborative Filtering
- 南大软院大神养成计划第二十一天
- int与float在内存中的存储形式_对比与转换