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;
}
原文地址:https://www.cnblogs.com/cheng07045406/p/3234809.html