东北师范大学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
2
3
4
5
61 ABCDEFGHIJKL 11700519 ZAYEXIWOVU 3072997 SOUGHT 1234567 THEQUICKFROG 0
Sample Output
复制代码
1
2
3
4
5LKEBA YOXUZ GHOST no solution
思想:枚举,把每种情况列举出来。最简单,在数据量大会卡。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53#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; }
复制代码
1
2
最后
以上就是甜美冰棍最近收集整理的关于求解密码问题C - RJ503求解密码问题的全部内容,更多相关求解密码问题C内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复