ACM乐园
Love Me,Love My Code!
posts - 53,  comments - 24,  trackbacks - 0
http://acm.hdu.edu.cn/showproblem.php?pid=1015

可以用两种方法做,一种是DFS,另一种是5重循环,都差不多一样,先是对输入的序列排序,这样第一个满足条件的就是我们所要的答案了。
下面我只写下5重循环
 1#include <iostream>
 2#include <algorithm>
 3#include <string>
 4
 5using namespace std;
 6
 7bool eq(int v,int w,int x,int y,int z,int t)
 8{
 9    if(v - w*+ x*x*- y*y*y*+ z*z*z*z*== t)
10        return true;
11    return false;
12}

13
14bool cmp(char a,char b)
15{
16    return a>b;
17}

18
19int 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==|| k==j)
36                        continue;
37                    for(l=0;l<len && !find;l++)
38                    {
39                        if(l==|| l==|| l==k)
40                            continue;
41                        for(m=0;m<len;m++)
42                        {
43                            if(m==|| m==|| m==|| 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 大大木马 阅读(714) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理



<2011年4月>
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

常用链接

留言簿(1)

随笔档案(53)

文章档案(2)

搜索

  •  

积分与排名

  • 积分 - 62451
  • 排名 - 353

最新评论

阅读排行榜

评论排行榜