标签:
#include <stdio.h> #include <iostream> #include <string> #include <string.h> #include <cmath> #include <math.h> #include <algorithm> using namespace std; #define Maxn 1000 int aim; int dir[Maxn]; bool visit[Maxn+1]; int Aim[Maxn]; string A; bool flag; bool cmp(int a,int b) { return a > b; } void dfs(int T) { if (flag) { return ; } if (T == 5) { if ( Aim[0] - pow(Aim[1],2) + pow(Aim[2],3) - pow(Aim[3],4) + pow(Aim[4],5) == aim) { flag = true; printf("%c%c%c%c%c\n",Aim[0]+‘A‘-1,Aim[1]+‘A‘-1,Aim[2]+‘A‘-1,Aim[3]+‘A‘-1,Aim[4]+‘A‘-1); } return ; } for(int i = 0; i < A.length(); i++) { if (!visit[i]) { Aim[T] = dir[i]; visit[i] = true; dfs(T+1); visit[i] = false; } } } int main() { while(cin >> aim >> A) { if (aim == 0) { break; } flag = false; for (int i = 0; i < A.length(); i++) { dir[i] = A[i] - ‘A‘ + 1; } sort(dir,dir+A.length(),cmp); for (int i = 0; i < A.length(); i++) { if (flag) { break; } memset(visit,false,sizeof(visit)); Aim[0] = dir[i]; visit[i] = true; dfs(1); } if (!flag) { printf("no solution\n"); } } }
标签:
原文地址:http://www.cnblogs.com/yakoazz/p/5725607.html