标签:
破解密码
把字符串从大到小排序,5个循环,验证目标函数
a-b*b+c*c*c-d*d*d*d+e*e*e*e*e == m为真时得到字符串 结束循环 输出
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cstdlib> 5 using namespace std; 6 7 int compare(const void*a,const void*b){ 8 return *(char*)b-*(char*)a; 9 } 10 int tar(int a,int b,int c,int d,int e,int m){ 11 if(a-b*b+c*c*c-d*d*d*d+e*e*e*e*e == m) 12 return 1; 13 return 0; 14 } 15 int main() 16 { 17 int m,j1,j2,j3,j4,j5,flag; 18 char c[30],s[6]; 19 int a[30]; 20 while(~scanf("%d %s",&m,c) && m && strcmp(c,"END")){ 21 memset(a,0,sizeof(a)); 22 flag=0; 23 int lc=strlen(c); 24 qsort(c,lc,sizeof(c[0]),compare); 25 for(int i=0;i<lc;i++){ 26 a[i]=c[i]-‘A‘+1; 27 } 28 for(j1=0;j1<lc;j1++){ 29 for(j2=0;j2<lc;j2++){ 30 if(j2 == j1) 31 continue; 32 else for(j3=0;j3<lc;j3++){ 33 if(j3==j1 || j3==j2) 34 continue; 35 else for(j4=0;j4<lc;j4++){ 36 if(j4==j3 || j4==j2 ||j4==j1) 37 continue; 38 else for(j5=0;j5<lc;j5++){ 39 if(j5==j4 || j5==j3 || j5==j2 || j5==j1) 40 continue; 41 else if(tar(a[j1],a[j2],a[j3],a[j4],a[j5],m) == 1){ 42 flag = 1;s[0]=c[j1];s[1]=c[j2];s[2]=c[j3];s[3]=c[j4];s[4]=c[j5];s[5]=‘\0‘; 43 j1=lc;//修改最外层的循环变量结束循环 44 } 45 } 46 } 47 } 48 } 49 } 50 if(flag==0)printf("no solution\n"); 51 else puts(s); 52 } 53 return 0; 54 }
标签:
原文地址:http://www.cnblogs.com/livelihao/p/5162951.html