\(^o^)/
Problem C
It took 15 minutes to wirte, and it was 5 minutes past my deadline.
#include <stdio.h> char nextToken; char input[100]; int P,Q,R,myindex=0; int not[3]={2,1,0}; int and[3][3]={{0,0,0},{0,1,1},{0,1,2}}; int or[3][3]={{0,1,2},{1,1,2},{2,2,2}}; int NOT(int a){ return not[a]; } int AND(int a,int b){ return and[a][b]; } int OR(int a,int b){ return or[a][b]; } void get_nextToken(){ nextToken=input[myindex]; myindex++; } int char2int(char ch){ return ch-'0'; } int is_instance(char ch){ if(ch=='P'|| ch=='Q'|| ch=='R'|| ch=='0'|| ch=='1'|| ch=='2' ){ return 1; } return 0; } int is_number(char ch){ if( ch=='0'|| ch=='1'|| ch=='2' ){ return 1; } return 0; } int get_value(){ int tmp,ret; if(is_number(nextToken)){ tmp=nextToken; get_nextToken(); return char2int(tmp); } switch(nextToken){ case 'P': ret=P; break; case 'Q': ret=Q; break; case 'R': ret=R; break; } get_nextToken(); return ret; } int get_formula(){ int ret=0,logic=0; get_nextToken(); if(is_instance(nextToken)){ return get_value(); } switch(nextToken){ case '-': logic=get_formula(); ret=NOT(logic); break; case '(': logic=get_formula(); switch(nextToken){ case '*': ret=AND(logic,get_formula()); break; case '+': ret=OR(logic,get_formula()); break; } get_nextToken(); break; } return ret; } main(){ int i,j,k,cnt,logic; while(1){ cnt=0; scanf("%s",input); if(input[0]=='.'){ break; } for(P=0;P<3;P++){ for(Q=0;Q<3;Q++){ for(R=0;R<3;R++){ myindex=0; logic=get_formula(); if(logic==2){ cnt++; } } } } printf("%d\n",cnt); } }