求解密码问题C - RJ503求解密码问题

东北师范大学11/18日算法课在线习题,地址nenuoj
Problem Description
给定一个整数n和一个由不同大写字母组成的字符串str(长度大于5、小于12),每一个字母在字母表中对应有一个序数(A=1,B=2,…,Z=26),从str中选择5个字母构成密码,例如选取的5个字母为v、w、x、y和z,他们要满足v的序号-(w的序数)2+(x的序数)3-(y的序数)4+(z的序数)5=n。例如,给定的n=1、字符串str为"ABCDEFGHIJKL",一个可能的解是“FIECB”,因为6-92+53-34+25=1,但这样的解可能有多个,最终结果是按字典序最大的那个,所以这里的正确答案是“LKEBA”.

Input
每一行为n和str,以输入n=0结束。

Output
每一行输出相应的密码,当密码不存在是输出“no solution”
Sample Input

1 ABCDEFGHIJKL
11700519 ZAYEXIWOVU
3072997 SOUGHT
1234567 THEQUICKFROG
0

Sample Output

LKEBA
YOXUZ
GHOST
no solution

思想:枚举,把每种情况列举出来。最简单,在数据量大会卡。

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin>>n;
    while(n!=0)
    {
    string str;
    cin>>str;
    int l = str.length();

    string max1 ="AAAAA";
    string tep;

    for(int v=l-1;v>=0;v--)
        for(int w=l-1;w>=0;w--)
            for(int x=l-1;x>=0;x--)
                for(int y=l-1;y>=0;y--)
                    for(int z=l-1;z>=0;z--)
    {
       if(v!=w&&v!=x&&v!=y&&v!=z&&w!=x&&w!=y&&w!=z&&x!=y&&x!=z&&y!=z)
       {
       int a=str[v]-64;
       int b=str[w]-64;
       int c=str[x]-64;
       int d=str[y]-64;
       int e=str[z]-64;
       if((a-b*b+c*c*c-d*d*d*d+e*e*e*e*e)==n)
       {
               tep  = str[v];
               tep  += str[w];
               tep  += str[x];
               tep  += str[y];
               tep  += str[z];
               if(tep>max1)
                max1 = tep;
       }
       }
    }
    if(max1=="AAAAA")
        cout<<"no solution"<<endl;
    else
        cout<<max1<<endl;

    cin>>n;
    }

    return 0;
}

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值