認真處理一番即可。
#include <iostream> #include <stdio.h> #include <cstring> #include <string> using namespace std; int main () { string s; while (cin >> s) { if (s=="END") break; //先找not int len=s.size(); int num=0; for (int x=0;len>x;x++) { if (s[x]=='!') num++; if (s[x]=='0') { if (num%2==1) s[x]='1'; num=0; } if (s[x]=='1') { if (num%2==1) s[x]='0'; } } while (1) { //找and int last_num=-1; int id2=-1; int id=-1; bool update=false; for (int x=0;len>x;x++) { if (s[x]=='*') { id=x; update=true; } else if (s[x]=='+') { id=-1; update=true; } else if (s[x]=='0') { if (last_num==-1) { //first number available last_num=0; id2=x; continue; } else if (id==-1) { //the last is "or" last_num=0; id2=x; continue; } else if (id!=-1){ s[id2]='#'; s[x]='#'; s[id]='0'; last_num=0; id2=id; id=-1; } } else if (s[x]=='1') { if (last_num==-1) { //first number available last_num=1; id2=x; continue; } else if (id==-1) { //the last is "or" last_num=1; id2=x; continue; } else if (id!=-1){ if (last_num==0)s[id]='0'; else s[id]='1'; s[id2]='#'; s[x]='#'; last_num=last_num; id2=id; id=-1; } } } //search or last_num=-1; id2=-1; id=-1; for (int x=0;len>x;x++) { if (s[x]=='+') { id=x; update=true; } else if (s[x]=='*') { id=-1; update=true; } else if (s[x]=='0') { if (last_num==-1) { //first number available last_num=0; id2=x; continue; } else if (id==-1) { //the last is "or" last_num=0; id2=x; continue; } else if (id!=-1){ s[id2]='@'; s[x]='@'; if (last_num==0) s[id]='0'; else s[id]='1'; last_num=last_num; id2=id; id=-1; } } else if (s[x]=='1') { if (last_num==-1) { //first number available last_num=1; id2=x; continue; } else if (id==-1) { //the last is "or" last_num=1; id2=x; continue; } else if (id!=-1){ if (last_num==1)s[id]='1'; else s[id]='1'; s[id2]='@'; s[x]='@'; last_num=1; id2=id; id=-1; } } } if(!update) break; } int ans=-1; for (int x=0;len>x;x++) { if (s[x]=='0') ans=0; else if (s[x]=='1') ans=1; } printf("%d\n",ans); } }
沒有留言:
張貼留言