#include <stdio.h>
#include <cstring>using namespace std;
char list[15];
int n;
bool mark[15];
char ans[6];
char ans_tmp[6];
int max = 0;
int num[6];int pow(int x, int y) //不知道为什么,math.h里的函数老是编译错误,只好自己写了。。
{
int tmp = 1;
for(int i = 1; i <= y; i++)
tmp *= x;
return tmp;
}bool check()
{
int target = 0;
for(int i = 0; i < 5; i++)
{
int tmp = ans_tmp[i]-'A'+1;
tmp = pow(tmp, i+1);
if(i % 2 == 0)
target += tmp;
else
target -= tmp;
}
if(target == n)
return true;
else
return false;
}void DFS(int num)
{
if(num >= 5)
{
if(check() && strcmp(ans_tmp, ans) > 0)
strcpy(ans, ans_tmp);
return;
}
for(int i = 0; i < strlen(list); i++)
{
if(mark[i] == false)
{
ans_tmp[num] = list[i];
mark[i] = true;
DFS(num+1);
mark[i] = false;
}
}
}int main()
{
while(~scanf("%d %s", &n, list) && n != 0)
{
memset(mark, 0, sizeof(mark));
memset(ans_tmp, '\0', sizeof(ans_tmp));
memset(ans, '\0', sizeof(ans));
DFS(0);
if(strlen(ans) == 0)
printf("no solution\n");
else
printf("%s\n", ans);
}
return 0;
}
Safecracker HDU - 1015-(DFS)
猜你喜欢
转载自blog.csdn.net/mch2869253130/article/details/82226658
今日推荐
周排行