题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1015
够暴力吧!
先排序,然后循环找。。。
#include <iostream>
#include <cstring>
using namespace std;
int cmp( const void *a , const void *b )
{
return *(char *)a - *(char *)b;
}
int main(int argc, char *argv[])
{
while(1)
{
int target;
char a[30] = " ";
cin >> target >> a;
if(target == 0 && strcmp(a,"END") == 0)
return 0;
qsort(a,strlen(a),sizeof(a[0]),cmp);
int len = strlen(a);
int x1,x2,x3,x4,x5;
int y1,y2,y3,y4,y5;
int max = 0;
for(int i = 0;i < len;i++)
{
for(int j = 0;j < len;j++)
{
for(int k = 0;k < len;k++)
{
for(int m = 0;m < len;m++)
{
for(int n = 0;n < len;n++)
{
if(a[i] != a[j] && a[i] != a[k] && a[i] != a[m] && a[i] != a[n] && a[k] != a[m] && a[k] != a[n] && a[m] != a[n])
{
x1 = a[i] - 64,x2 = a[j] - 64,x3 = a[k] - 64,x4 = a[m] - 64,x5 = a[n] - 64;
if(x1 - x2*x2 + x3*x3*x3 - x4*x4*x4*x4 + x5*x5*x5*x5*x5 == target)
{
y1 = i,y2 = j, y3 = k,y4 = m,y5 = n;
max = 1;
}
}
}
}
}
}
}
if(max == 0)
cout << "no solution" << endl;
else
cout << a[y1] << a[y2] << a[y3] << a[y4] << a[y5] << endl;
}
return 0;
}