写完代码发现WA,无奈之下看了讨论区中的测试数据,觉得自己弱爆了!没理解到底什么是WFF,太急于写代码了!
先把错误代码贴出来,提醒我以后一定把题目审清楚再做题!
#include <iostream> #include <stack> #include <stdlib.h> #include <stdio.h> #include <string> using namespace std; int p,q,r,s,t; int trans(char tmp) { if(tmp == '0') return 0; else if(tmp == '1') return 1; else if(tmp == 'p') return p; else if(tmp == 'q') return q; else if(tmp == 'r') return r; else if(tmp == 's') return s; else if(tmp == 't') return t; } char transR(int tmp) { if(tmp == 0) return '0'; else if(tmp == 1) return '1'; } int main() { freopen("in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取 freopen("out.txt","w",stdout); //输出重定向,输出数据将保存在out.txt文件中 string line; while(cin >> line) { if(line.length() == 1 && line[0] == '0') break; int flag = 1; string origin = line; if(line.length() == 1) flag = 0; for(int i = 0;i < 32;++i) { t = i & 1; s = (i >> 1) & 1; r = (i >> 2) & 1; q = (i >> 3) & 1; p = (i >> 4) & 1; line = origin; int nowSite = line.length() - 1; while(nowSite > 0) { if(line[nowSite - 1] == 'N') { line[nowSite - 1] = transR(!trans(line[nowSite])); nowSite--; } else { int tmp1,tmp2; tmp1 = trans(line[nowSite - 1]); tmp2 = trans(line[nowSite]); if(line[nowSite - 2] == 'K') { line[nowSite - 2] = transR(tmp1 & tmp2); } else if(line[nowSite - 2] == 'A') { line[nowSite - 2] = transR(tmp1 | tmp2); } else if(line[nowSite - 2] == 'C') { line[nowSite - 2] = transR(!tmp1 | tmp2); } else if(line[nowSite - 2] == 'E') { if(tmp1 == tmp2) line[nowSite - 2] = '1'; else line[nowSite - 2] = '0'; } nowSite -=2; } } if(line[0] == '0') { flag = 0; //printf("%d\n",p); break; } } if(flag) printf("tautology\n"); else printf("not\n"); } return 0; }
#include <iostream> using namespace std; char str[101]; int pos; bool judge( char str[], int value ) { pos++; switch ( str[pos] ) { case 'p': return value&1; case 'q': return (value>>1)&1; case 'r': return (value>>2)&1; case 's': return (value>>3)&1; case 't': return (value>>4)&1; case 'K': return judge(str,value)&judge(str,value); case 'A': return judge(str,value)|judge(str,value); case 'N': return !judge(str,value); case 'C': return (!judge(str,value))|judge(str,value); case 'E': return judge(str,value)==judge(str,value); default:; } } int main() { bool mark; while ( cin >> str && str[0] != '0' ) { mark = true; for ( int i = 0; i < 32; i++ ) { pos = -1; if ( !judge(str,i) ) { mark = false; break; } } if ( mark ) cout << "tautology" << endl; else cout << "not" << endl; } return 0; } 代码2: #include <iostream> using namespace std; int Nth_Char; char str[101]; bool step ( char str[101], int logicValue ) { Nth_Char++; switch ( str[Nth_Char] ) { case 'p': return logicValue & 1; case 'q': return ( logicValue >> 1 ) & 1; case 'r': return ( logicValue >> 2 ) & 1; case 's': return ( logicValue >> 3 ) & 1; case 't': return ( logicValue >> 4 ) & 1; case 'N': return !step ( str, logicValue ); case 'K': return step ( str, logicValue ) & step ( str, logicValue ); case 'A': return step ( str, logicValue ) | step ( str, logicValue ); case 'C': return !step (str, logicValue ) | step ( str, logicValue ); case 'E': return step ( str, logicValue ) == step ( str, logicValue ); default:; } } bool judge ( char str[101] ) { for ( int i = 0; i < 32; i++ ) { Nth_Char = -1; if ( !step ( str, i ) ) return false; } return true; } int main() { while ( cin >> str && str[0] != '0' ) { if ( judge ( str ) ) cout << "tautology" << endl; else cout << "not" << endl; } return 0; }
给一个布尔表达式,判断是不是恒真。 我是枚举然后用栈做的,判断栈顶俩元素,是布尔变量就直接算,不是就接着往栈里扔。 p.s.这让我想起了本学期那节逻辑课……因为样例就是p | ( !p )是恒真表达式………… /* ID: SummerDAway PROG: 3295 LANG: C++ OJ: POJ.ORG */ #include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath> #include <algorithm> #include <vector> #include <string> #include <iostream> #include <sstream> #include <map> #include <set> #include <queue> #include <stack> #include <fstream> #include <numeric> #include <iomanip> #include <bitset> #include <list> #include <stdexcept> #include <functional> #include <utility> #include <ctime> using namespace std ; char a[200] ; map<char,int> f ; int top ; char Stack[200] ; char Trans( bool x ) { if ( x ) return '1' ; else return '0' ; } void calc( char a , char x , char y ) { top -= 3 ; if ( a == 'K' ) Stack[top++] = Trans( f[x] & f[y] ) ; if ( a == 'A' ) Stack[top++] = Trans( f[x] | f[y] ) ; if ( a == 'C' ) Stack[top++] = Trans( (!f[x]) | f[y] ) ; if ( a == 'E' ) Stack[top++] = Trans( !( f[x] ^ f[y] ) ) ; return ; } bool judge( ) { int len = strlen( a ) ; for ( int i = 0 ; i < len ; i++ ) { Stack[top++] = a[i] ; bool flag = false ; while ( top > 1 && !flag ) { flag = true ; char x = Stack[top-1] ; char y = Stack[top-2] ; if ( ( f[x] >= 0 && f[y] >= 0 ) ) { calc( Stack[top-3] , y , x ) ; flag = false ; } if ( f[x] >= 0 && y == 'N' ) { top -= 2 ; Stack[top++] = Trans( !f[x] ) ; flag = false ; } } } if ( Stack[0] == '1' ) return true ; else return false ; } int main() { freopen( "3295.in" , "r" , stdin ) ; freopen( "3295.out" , "w" , stdout ) ; gets( a ) ; while ( a[0] != '0' ) { f['1'] = 1 , f['0'] = 0 ; f['A'] = -1 , f['K'] = -1 , f['C'] = -1 , f['E'] = -1 , f['N'] = -1 ; bool flag = true ; for ( int t = 0 ; t < 2 ; t++ ) for ( int b = 0 ; b < 2 ; b++ ) for ( int c = 0 ; c < 2 ; c++ ) for ( int d = 0 ; d < 2 ; d++ ) for ( int e = 0 ; e < 2 ; e++ ) { f['p'] = t , f['q'] = b , f['r'] = c , f['s'] = d , f['t'] = e ; top = 0 ; if ( !judge() ) { flag = false ; } } if ( flag ) printf( "tautology\n" ) ; else printf( "not\n" ) ; gets( a ) ; } return 0 ; }