标签:break set == 不能 出现 main scan bre math
给T个式子,其中有一个数字是?,让确定?代表哪个数字
负号,前导0,出现过的数字不能是?
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <queue> #include <algorithm> using namespace std; char s[33]; int vis[11]; bool checkzero(int t) { if(s[t] == ‘-‘) t++; if((s[t] == ‘?‘) && ((s[t+1] >= ‘0‘ && s[t+1] <= ‘9‘) || s[t+1] == ‘?‘) ) return 1; //no return 0; } int main() { int T; scanf("%d", &T); while(T--) { scanf("%s", s); int len = strlen(s); long long a, b, c; memset(vis, 0, sizeof(vis)); int op, idx, zf; for(int i = 0; i < len; i++) { if(s[i] >= ‘0‘ && s[i] <= ‘9‘) vis[s[i] - ‘0‘] = 1;//等式中出现过的数不能是? if(s[i] == ‘=‘) idx = i; } int ans, flag = 0; for(int j = 0; j <= 9; j++)//枚举?的所有可能情况 { if(vis[j] == 1) continue; a = 0, b = 0, c = 0; zf = 1;//负号坑 for(int i = 0; i < len; i++) { if(s[i] == ‘-‘ && i == 0) zf = -1; else if(s[i] == ‘+‘ || s[i] == ‘-‘ || s[i] == ‘*‘) { op = i; break; } else if(s[i] == ‘?‘) a = a * 10 + j; else a = a * 10 + s[i] - ‘0‘; } if(j == 0 && checkzero(0)) continue; a = a * zf; zf = 1; for(int i = op + 1; i < idx; i++) { if(s[i] == ‘-‘ && i == op+1) zf = -1; else if(s[i] == ‘?‘) b = b * 10 + j; else b = b * 10 + s[i] - ‘0‘; } if(j == 0 && checkzero(op+1)) continue; b = b * zf; zf = 1; for(int i = idx + 1; i < len; i++) { if(s[i] == ‘-‘ && i == idx+1) zf = -1; else if(s[i] == ‘?‘) c = c * 10 + j; else c = c * 10 + s[i] - ‘0‘; } if(j == 0 && checkzero(idx+1)) continue; c = c * zf; if(s[op] == ‘+‘ && a + b == c) {ans = j; flag = 1; break;} if(s[op] == ‘-‘ && a - b == c) {ans = j; flag = 1; break;} if(s[op] == ‘*‘ && a * b == c) {ans = j; flag = 1; break;} } if(flag) cout << ans << endl; else cout << "-1" << endl; } return 0; }
标签:break set == 不能 出现 main scan bre math
原文地址:https://www.cnblogs.com/pinkglightning/p/8978024.html