zoukankan      html  css  js  c++  java
  • HDU 1015 Safecracker

    破解密码

    把字符串从大到小排序,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]='';
    43                                 j1=lc;//修改最外层的循环变量结束循环
    44                             }
    45                         }
    46                     }
    47                 }
    48             }
    49         }
    50         if(flag==0)printf("no solution
    ");
    51         else puts(s);
    52     }
    53     return 0;
    54 }
    ---------------- 人们生成的最美好的岁月其实就是最痛苦的时候,只是事后回忆起来的时候才那么幸福。
  • 相关阅读:
    自定义key解决zabbix端口监听取值不准确的问题
    Redis——主从同步原理
    Leetcode 24——Swap Nodes in Pairs
    Struts2——第一个helloworld页面
    Leetcode 15——3Sum
    Leetcode 27——Remove Element
    C#简单入门
    Leetcode 12——Integer to Roman
    Leetcode 6——ZigZag Conversion
    eclipse如何debug调试jdk源码(任何源码)并显示局部变量
  • 原文地址:https://www.cnblogs.com/livelihao/p/5162951.html
Copyright © 2011-2022 走看看