POJ 3295 Tautology
题意:逻辑运算题,最多有5个变量,依次枚举就可以AC了
k | A | N | C | E |
a && b | a || b | !a | !a || b | a ==b |
思路:之前是用递归写的,但是WA或是超时,郁闷。。。后来在网上参考了别人的代码,用栈一次AC。
10962407 | NY_lv10 | 3295 | Accepted | 208K | 16MS | C++ | 1648B | 2012-10-27 20:08:57 |
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <stack> //#include <fstream> #include <string> using namespace std; //ofstream out("1.txt"); string str; stack<int> stk; bool v[6]; bool tautology; bool GetSult(/*int p*/) { int a, b; for (int i = str.length()-1; i>=0; i--) { switch(str[i]) { case 'K': { a = stk.top(); stk.pop(); b = stk.top(); stk.pop(); stk.push(a && b); //GetSult(p+1) && GetSult(p+2); break; } case 'A': { a = stk.top(); stk.pop(); b = stk.top(); stk.pop(); stk.push(a || b); break; //return GetSult(p+1) || GetSult(p+2); } case 'N': { a = stk.top(); stk.pop(); stk.push(!a); break; //return !GetSult(p+1); } // return !v[str[p+1]-'p']; case 'C': { a = stk.top(); stk.pop(); b = stk.top(); stk.pop(); stk.push(!a || b); break; //return (!GetSult(p+1)) || GetSult(p+2); } case 'E': { a = stk.top(); stk.pop(); b = stk.top(); stk.pop(); stk.push(a == b); break; //return GetSult(p+1) == GetSult(p+2); } default: stk.push( v[str[i]-'p']); } } return stk.top(); } int main() { int i, j; while (cin>>str && str!="0") { while (!stk.empty()) stk.pop(); tautology = true; for (i=0; i<=31; i++) { for (j=0; j<5; j++) v[j] = (i>>j) % 2; if (!GetSult(/*0*/)) { tautology = false; goto L; } } L: if (tautology) { // out<<"tautology"<<endl; cout<<"tautology"<<endl; } else { // out<<"not"<<endl; cout<<"not"<<endl; } } return 0; }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <stack> #include <fstream> #include <string> using namespace std; ofstream out("1.txt"); string str; stack<int> stk; bool v[6]; bool tautology; bool GetSult(int p) { switch(str[p]) { case 'K': return GetSult(p+1) && GetSult(p+2); case 'A': return GetSult(p+1) || GetSult(p+2); case 'N': return !GetSult(p+1); // return !v[str[p+1]-'p']; case 'C': return (!GetSult(p+1)) || GetSult(p+2); case 'E': return GetSult(p+1) == GetSult(p+2); default: return v[str[p]-'p']; } } int main() { int i, j; while (cin>>str && str!="0") { tautology = true; for (i=0; i<=31; i++) { for (j=0; j<5; j++) v[j] = (i>>j) % 2; if (!GetSult(0)) { tautology = false; goto L; } } L: if (tautology) { out<<"tautology"<<endl; cout<<"tautology"<<endl; } else { out<<"not"<<endl; cout<<"not"<<endl; } } return 0; }