http://acm.hdu.edu.cn/showproblem.php?pid=1015可以用两种方法做,一种是DFS,另一种是5重循环,都差不多一样,先是对输入的序列排序,这样第一个满足条件的就是我们所要的答案了。
下面我只写下5重循环
1
#include <iostream>
2
#include <algorithm>
3
#include <string>
4
5
using namespace std;
6
7
bool eq(int v,int w,int x,int y,int z,int t)
8

{
9
if(v - w*w + x*x*x - y*y*y*y + z*z*z*z*z == t)
10
return true;
11
return false;
12
}
13
14
bool cmp(char a,char b)
15

{
16
return a>b;
17
}
18
19
int main()
20

{
21
int t,i,j,k,l,m,find,len;
22
string str;
23
while(cin>>t>>str,t,str!="END")
24
{
25
find=0;
26
sort(str.begin (),str.end(),cmp);
27
len=str.length ();
28
for(i=0;i<len && !find;i++)
29
for(j=0;j<len && !find;j++)
30
{
31
if(j==i)
32
continue;
33
for(k=0;k<len && !find;k++)
34
{
35
if(k==i || k==j)
36
continue;
37
for(l=0;l<len && !find;l++)
38
{
39
if(l==i || l==j || l==k)
40
continue;
41
for(m=0;m<len;m++)
42
{
43
if(m==i || m==j || m==k || m==l)
44
continue;
45
if(eq(str[i]-'A'+1,str[j]-'A'+1,str[k]-'A'+1,str[l]-'A'+1,str[m]-'A'+1,t))
46
{
47
cout<<str[i]<<str[j]<<str[k]<<str[l]<<str[m]<<endl;
48
find=1;
49
break;
50
}
51
}
52
}
53
}
54
}
55
if(!find)
56
cout<<"no solution\n";
57
}
58
return 0;
59
}
posted on 2011-04-01 16:58
大大木马 阅读(723)
评论(0) 编辑 收藏 引用