题目:http://acm.hdu.edu.cn/showproblem.php?pid=1015
暴力一点还是可以的....
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdio>
using namespace std;
int f(char a,char b,char c,char d,char e)
{
char aa[5]={a,b,c,d,e};
for(int i=0;i<5;i++)
for(int j=i+1;j<5;j++)
if(aa[i]==aa[j])
return 0;
return 1;
}
int main()
{
char a[30];
long int n;
while(scanf("%ld%s",&n,a),(n||strcmp(a,"END")!=0))
{
long int s=0,flag=1,temp;
sort(a,a+strlen(a));
for(int i=strlen(a)-1;i>=0&&flag;i--)
{
s=0;
s+=(a[i]-'A'+1);
for(long int j=strlen(a)-1,temp1=s;j>=0&&flag;j--)
{
s=temp1-(a[j]-'A'+1)*(a[j]-'A'+1);
for(long int k=strlen(a)-1,temp2=s;k>=0&&flag;k--)
{
temp=(a[k]-'A'+1);
s=temp2+temp*temp*temp;
for(long int l=strlen(a)-1,temp3=s;l>=0&&flag;l--)
{
temp=(a[l]-'A'+1);
s=temp3-temp*temp*temp*temp;
for(long int m=strlen(a)-1,temp4=s;m>=0;m--)
{
temp=(a[m]-'A'+1);
s=temp4+temp*temp*temp*temp*temp;
if(s==n&&f(a[i],a[j],a[k],a[l],a[m])==1)
{
printf("%c%c%c%c%c\n",a[i],a[j],a[k],a[l],a[m]);
flag=0;
break;
}
}
}
}
}
if(flag)
{
printf("no solution\n");
}
return 0;
}