UVa-817 According to Bartjens
2000=要输出IMPOSSIBLE
1 #include<bits/stdc++.h> 2 #define _for(i,a,b) for(int i = (a);i < (b);i ++) 3 #pragma GCC optimize(2) 4 5 using namespace std; 6 string input; 7 set<string> rnt; 8 9 int kase = 1; 10 11 bool judge(string a) 12 { 13 stack<int> s; 14 int tmp = 0; 15 int i; 16 if(a[0]=='0'&&isdigit(a[1])) 17 return false; 18 for(i = 0; isdigit(a[i]); i ++) 19 { 20 tmp = tmp*10+a[i]-'0'; 21 } 22 s.push(tmp); 23 24 for(; a[i]!='=';) 25 { 26 tmp = 0; 27 if(a[i]=='+') 28 { 29 if(a[i+1]=='0'&&isdigit(a[i+2])) 30 return false; 31 for(i ++; isdigit(a[i]); i ++) 32 tmp = tmp*10+a[i]-'0'; 33 s.push(tmp); 34 } 35 else if(a[i]=='-') 36 { 37 if(a[i+1]=='0'&&isdigit(a[i+2])) 38 return false; 39 for(i ++; isdigit(a[i]); i ++) 40 tmp = tmp*10+a[i]-'0'; 41 s.push(-tmp); 42 } 43 else if(a[i]=='*') 44 { 45 if(a[i+1]=='0'&&isdigit(a[i+2])) 46 return false; 47 for(i ++; isdigit(a[i]); i ++) 48 tmp = tmp*10+a[i]-'0'; 49 int tmp2 = s.top(); 50 s.pop(); 51 s.push(tmp2*tmp); 52 } 53 } 54 int sum = 0; 55 while(!s.empty()) 56 { 57 sum += s.top(); 58 s.pop(); 59 } 60 return sum==2000; 61 } 62 63 const char op[] {'+','-','*'}; 64 void dfs(int d,string a) 65 { 66 if(a[d+1]=='=') 67 { 68 if(judge(a)) 69 { 70 rnt.insert(a); 71 } 72 return ; 73 } 74 75 if(isdigit(a[d])) 76 { 77 int i; 78 for(i = 0;i < 4;i ++) 79 { 80 if(i==0) 81 { 82 dfs(d+1,a); 83 } 84 else 85 { 86 string b = a.insert(d+1,1,op[i-1]); 87 dfs(d+1,b); 88 a.erase(d+1, 1); 89 } 90 } 91 } 92 else 93 dfs(d+1,a); 94 } 95 void solve() 96 { 97 rnt.clear(); 98 if(input.size()==2||input=="2000=") 99 return ; 100 dfs(0,input); 101 } 102 void printRnt() 103 { 104 printf("Problem %d\n",kase ++); 105 for(auto s:rnt) 106 printf(" %s\n",s.c_str()); 107 if(rnt.size()==0) 108 printf(" IMPOSSIBLE\n"); 109 } 110 int main() 111 { 112 while(getline(cin,input)) 113 { 114 if(input[0]=='=') 115 break; 116 solve(); 117 printRnt(); 118 } 119 return 0; 120 }