1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
#include <iostream> #include <algorithm> #include <queue> #include <vector> #include <cstdlib> #include <cstdio> #include <string> #include <cstring> #include <ctime> #include <iomanip> #include <cmath> #include <set> #include <map> #include <list> #include <stack> using namespace std; int main(){ long long target; string toProc; while (cin >> target >> toProc) { if (target == 0 && toProc == "END") { break; } vector<bool> toru; for (int i = 1; i <= 5; i++) toru.push_back(true); for (int i = 1; i <= toProc.size()-5; i++) { toru.push_back(false); } sort(toru.begin(), toru.end()); string res; do{ string tmp; for (int i = 0; i < toProc.size();i++) { if (toru[i]) { tmp += toProc[i]; } } sort(tmp.begin(),tmp.end()); do{ long long judge = 0; judge += tmp[0]-'A'+1; judge += (tmp[1]-'A'+1)*(tmp[1]-'A'+1)*(-1); judge += (tmp[2]-'A'+1)*(tmp[2]-'A'+1)*(tmp[2]-'A'+1); judge += (tmp[3]-'A'+1)*(tmp[3]-'A'+1)*(tmp[3]-'A'+1)*(tmp[3]-'A'+1)*(-1); judge += (tmp[4]-'A'+1)*(tmp[4]-'A'+1)*(tmp[4]-'A'+1)*(tmp[4]-'A'+1)*(tmp[4]-'A'+1); if (judge == target) { if (tmp > res) { res = tmp; } } }while (next_permutation(tmp.begin(), tmp.end())) ; }while (next_permutation(toru.begin(), toru.end())); if (!res.size()) { cout << "no solution" << endl; continue; } cout << res << endl; } return 0; } |
用STL,next_permutation把所有可能性搞出来注意检验就可以爆过去了。