zoukankan      html  css  js  c++  java
  • Safecracker

    简化:
    #include <iostream>
    #include <string>
    #include <list>
    #include <stdlib.h>
    #include<functional>
    #include <algorithm>
    using namespace std;
    bool un_equal(char a,char b,char c,char d,char e) //要求字符互不相同
    {
        if(a - b && b - c && c - d && d - e && a - c && a - d && a - e && b - d && c - e && b - e)
            return true;
        else
            return false;
    }
    int cmp(void *a,void *b)
    {
        return  1;
    }
    int main()
    {
        char str[20];
        char result_str[6];
        char result_temp[6];
        int d[6][26];
        int target,i,j;
        int len,v,w,x,y,z;
        for(i=0;i<26;i++)
            d[1][i] = i+1;
        for(j =2;j<6;j++)
            for(i =0;i<26;i++)
                d[j][i]=d[j-1][i] * d[1][i];  //预先计算好,多次幂乘积,空间换时间,加快速度
    
        while(scanf("%d %s",&target,str)!=EOF)
        {
            if(target==0 && strcmp(str,"END")==0)
                break;
            int k=0;
            len = strlen(str);
            for(v = 0;v<len;v++)
                for(w= 0;w<len;w++)
                    for(x=0;x<len;x++)
                        for(y=0;y<len;y++)
                            for(z=0;z<len;z++)
                            {
                                int sum = d[1][str[v]-'A'] - d[2][str[w]-'A'] + d[3][str[x]-'A']- d[4] [str[y]-'A']+ d[5][str[z]-'A'];
                                if(un_equal(str[v],str[w],str[x],str[y],str[z]) && (sum==target))
                                {
                                    result_temp[0]= str[v];
                                    result_temp[1]= str[w];
                                    result_temp[2]= str[x];
                                    result_temp[3]= str[y];
                                    result_temp[4]= str[z];
                                    result_temp[5]=0;
                                    if(k==0)
                                    {
                                        strcpy(result_str,result_temp);
                                    }
                                    else if(strcmp(result_temp,result_str) > 0)
                                        strcpy(result_str,result_temp);
                                    k++;
                                }
                            }
                if(k==0)
                    printf("no solution
    ");
                else
                {
                    printf("%s
    ",result_str);
                }
        }
        return 0;
    }
    //冒泡字符串排序
    #include <iostream>
    #include <string>
    #include <list>
    #include <stdlib.h>
    #include<functional>
    #include <algorithm>
    using namespace std;
    bool un_equal(char a,char b,char c,char d,char e) //要求字符互不相同
    {
        if(a - b && b - c && c - d && d - e && a - c && a - d && a - e && b - d && c - e && b - e)
            return true;
        else
            return false;
    }
    int main()
    {
        char str[20];
        char result_str[20000][6];
        int d[6][26];
        int target,i,j;
        int len,v,w,x,y,z;
        for(i=0;i<26;i++)
            d[1][i] = i+1;
        for(j =2;j<6;j++)
            for(i =0;i<26;i++)
                d[j][i]=d[j-1][i] * d[1][i];  //预先计算好,多次幂乘积
    
        while(scanf("%d %s",&target,str)!=EOF)
        {
            if(target==0 && strcmp(str,"END")==0)
                break;
            int k=0;
            len = strlen(str);
            for(v = 0;v<len;v++)
                for(w= 0;w<len;w++)
                    for(x=0;x<len;x++)
                        for(y=0;y<len;y++)
                            for(z=0;z<len;z++)
                            {
                                int sum = d[1][str[v]-'A'] - d[2][str[w]-'A'] + d[3][str[x]-'A']- d[4] [str[y]-'A']+ d[5][str[z]-'A'];
                                if(un_equal(str[v],str[w],str[x],str[y],str[z]) && (sum==target))
                                {
                                    result_str[k][0]= str[v];
                                    result_str[k][1]= str[w];
                                    result_str[k][2]= str[x];
                                    result_str[k][3]= str[y];
                                    result_str[k][4]= str[z];
                                    result_str[k][5]=0;
                                    k++;
                                }
                            }
                if(k==0)
                    printf("no solution
    ");
                else
                {
                    for(int i =0;i<k-1;i++)
                        for(int j=0;j<k-i;j++)
                            if(strcmp(result_str[j],result_str[j+1])<=0)  //字符串数组,冒泡排序
                            {
                                char temp[6];
                                strcpy(temp,result_str[j]);
                                strcpy(result_str[j],result_str[j+1]);
                                strcpy(result_str[j+1],temp);
                            }
                        printf("%s
    ",result_str[0]);
                }
        }
        return 0;
    }
    下面是利用用stl
    #include <iostream>
    #include <string>
    #include <list>
    #include <stdlib.h>
    #include<functional>
    #include <algorithm>
    using namespace std;
    bool un_equal(char a,char b,char c,char d,char e) //要求字符互不相同
    {
        if(a - b && b - c && c - d && d - e && a - c && a - d && a - e && b - d && c - e && b - e)
            return true;
        else
            return false;
    }
    int cmp(void *a,void *b)
    {
        return  1;
    }
    int main()
    {
        char str[20];
        int d[6][26];
        int target,i,j;
        int len,v,w,x,y,z;
        list<string>solution_list;
        for(i=0;i<26;i++)
            d[1][i] = i+1;
        for(j =2;j<6;j++)
            for(i =0;i<26;i++)
                d[j][i]=d[j-1][i] * d[1][i];  //预先计算好,多次幂乘积
    
        while(scanf("%d %s",&target,str)!=EOF)
        {
            if(target==0 && strcmp(str,"END")==0)
                break;
            solution_vector.clear();
            len = strlen(str);
            for(v = 0;v<len;v++)
                for(w= 0;w<len;w++)
                    for(x=0;x<len;x++)
                        for(y=0;y<len;y++)
                            for(z=0;z<len;z++)
                            {
                                int sum = d[1][str[v]-'A'] - d[2][str[w]-'A'] + d[3][str[x]-'A']- d[4] [str[y]-'A']+ d[5][str[z]-'A'];
                                if(un_equal(str[v],str[w],str[x],str[y],str[z]) && (sum==target))
                                {
                                    string temp;
                                    temp+=str[v];
                                    temp+=str[w];
                                    temp+=str[x];
                                    temp+=str[y];
                                    temp+=str[z];
                                    solution_list.push_back(temp);
                                }
                            }
                if(solution_list.empty())
                    printf("no solution
    ");
                else
                {
                    solution_list.sort(greater<string>());
                    cout << solution_list.begin()->data() << endl;
                }
        }
        return 0;
    }
  • 相关阅读:
    window2003 安全配置
    VirusScan7.0使用说明
    管理者必读的四个小故事
    iis权限设置
    电信增值业务寻找合作伙伴
    ERP项目管理12要点
    抽闲破个案,放松一下(1)
    网站策划,网站建设的重中之重
    企业及时通讯软件源代码销售,功能类似QQ/UC/贸易通
    软件项目实施规范小结
  • 原文地址:https://www.cnblogs.com/cheng07045406/p/3234809.html
Copyright © 2011-2022 走看看