标签:%s end printf span main scanf dfs set div
解题思路:1 五重循环
2 dfs, 就写下这个代码了(参考了discuss 中的解题)
#include<bits/stdc++.h> using namespace std; int s[20], vis[20], ans[10]; int n; int len, ok = 0; int check() { int a = ans[0], b = ans[1], c = ans[2], d = ans[3], e = ans[4]; if(a - b*b + c*c*c - d*d*d*d + e*e*e*e*e == n) return 1; return 0; } int dfs(int d) { if(d == 5) { if(check()) ok = 1; return 0; } for(int i = 0; i < len; i++) { if(!vis[i]) { vis[i] = 1; ans[d] = s[i]; dfs(d + 1); if(ok == 1) return 0; vis[i] = 0; } } } int main() { //freopen("ain.txt", "r", stdin); char str[20]; while(scanf("%d%s", &n, str) && strcmp(str, "END")) { ok = 0; memset(vis, 0, sizeof(vis)); len = strlen(str); for(int i = 0; i < len; i++) s[i] = str[i] - ‘A‘ + 1; sort(s, s + len, greater<int>()); dfs(0); if(ok) { for(int k = 0; k < 5; k++) printf("%c", ans[k] + ‘A‘ - 1); cout << endl; } else cout << "no solution" << endl; } return 0; }
标签:%s end printf span main scanf dfs set div
原文地址:https://www.cnblogs.com/bearcarl/p/8838635.html