#include<iostream> #include<vector> #include<bits/stdc++.h> using namespace std; vector<int> pos; string s; bool judge(string ss){ if(ss[0] == '0' || ss[3] == '0' || ss[6] == '0'){ return false; } if(atoi(ss.substr(0, 2).c_str()) + atoi(ss.substr(3, 2).c_str()) == atoi(ss.substr(6).c_str())){ return true; } return false; } void DFS(int index, string ss){ if(index == pos.size()){ if(judge(ss)){ cout << ss << endl; } return; } for(char i = '0'; i <= '9'; i++){ ss[pos[index]] = i; DFS(index + 1, ss); ss[pos[index]] = '?'; } } int main(){ while(cin >> s){ pos.clear(); for(int i = 0; i < s.length(); i++){ if(s[i] == '?') pos.push_back(i); } DFS(0, s); } return 0; }
先把是问号的位置存起来,然后搜索。
浙公网安备 33010602011771号