HDoj 1015 Safecracker

Problem Description
=== Op tech briefing, 2002/11/02 06:42 CST ===
"The item is locked in a Klein safe(克莱因保险柜) behind a painting in the second-floor library.
Klein safes are extremely rare(稀有的); most of them, along with Klein and his factory(伴随着克莱因和他的函数), were destroyed in World War II.
Fortunately old Brumbaugh(布伦博,人名) from research knew Klein's secrets and wrote them down before he died.
A Klein safe has two distinguishing features(特点): a combination lock(combination lock,密码锁) that uses letters instead of numbers, and an engraved quotation(深深刻入的引用语)
on the door.
A Klein quotation always contains between five and twelve distinct uppercase(大写字母的) letters, usually at the beginning of sentences, and mentions(提到了) one or more numbers.
Five of the uppercase letters form(形式,排列) the combination that opens the safe.
By combining the digits(数字) from all the numbers in the appropriate way you get a numeric target. (The details of constructing the target number are classified.)
To find the combination you must select five letters v, w, x, y, and z that satisfy the following equation,
where each letter is replaced by its ordinal position in the alphabet (A=1, B=2, ..., Z=26). The combination is then vwxyz.
If there is more than one solution then the combination is the one that is lexicographically greatest, i.e., the one that would appear last in a dictionary."

v - w^2 + x^3 - y^4 + z^5 = target

"For example, given target 1 and letter set ABCDEFGHIJKL, one possible solution is FIECB, since 6 - 9^2 + 5^3 - 3^4 + 2^5 = 1.
There are actually several solutions in this case, and the combination turns out to be LKEBA. Klein thought it was safe to encode the combination within the engraving,
because it could take months of effort to try all the possibilities even if you knew the secret. But of course computers didn't exist then."

=== Op tech directive, computer division, 2002/11/02 12:30 CST ===

"Develop a program to find Klein combinations in preparation for field deployment.
Use standard test methodology as per departmental regulations. Input consists of one or more lines containing a positive integer target less than twelve million,
a space, then at least five and at most twelve distinct uppercase letters. The last line will contain a target of zero and the letters END; this signals the end of the input.
For each line output the Klein combination, break ties with lexicographic order, or 'no solution' if there is no correct combination. Use the exact format shown below."

Sample Input
1 ABCDEFGHIJKL
11700519 ZAYEXIWOVU
3072997 SOUGHT
1234567 THEQUICKFROG
0 END

Sample Output
LKEBA
YOXUZ
GHOST
no solution

Source
Mid-Central USA 2002

Recommend
JGShining | We have carefully selected several similar problems for you: 1010 1016 1239 1238 1072

题目解析,本题主要考察的是全排列,具体学到的知识点有:

1如何将一个字符串按照ABCD从大到小排列       没有函数,自己手写一个排序

2如何进行组合       利用多重for循环遍历

3如何退出for循环     本题中用的函数加return语句,有更多的方法在收藏里

4如何进行全排列      用c++的stl库,有两个函数a找比当前序列字典顺序小的下一个全排列b找比当前序列大的下一个全排列,记得要用do while循环,否则第一个序列就不会显示。

5带引用参数的函数,在声明时变量类型之后要加引用符号

C语言代码如下:

#include<stdio.h>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
char a[5];
int solution(long long,string);
void BubbleSort(string &);    //带引用参数的函数声明 
int main()
{
    int long long n=0;
    string str="";

    while(cin>>n)
    {
        cin>>str;
        
        //cout<<str;
        if(n==0&&str=="END")
            break;
        else
            BubbleSort(str);    //对这个字符串按照字母从大到小排序 
            solution(n,str);
        
    }
    return 0;    
 }
 int solution(long long n,string str)
 {
     for(int i=str.size()-1;i>=4;i--)
                for(int j=i-1;j>=3;j--)
                    for(int k=j-1;k>=2;k--)
                        for(int l=k-1;l>=1;l--)
                            for(int m=l-1;m>=0;m--)
                            {
                                a[0]=str[i];
                                a[1]=str[j];
                                a[2]=str[k];
                                a[3]=str[l];
                                a[4]=str[m];
                                do
                                {
                                    if( (a[0]-64) - (long long)pow( (a[1]-64),2)  + (long long)pow( (a[2]-64),3) - (long long)pow( (a[3]-64),4)   + (long long)pow( (a[4]-64),5) ==n)
                                    {
                                        printf("%c%c%c%c%c\n",a[0],a[1],a[2],a[3],a[4]);
                                        return 1; 
                                    }    
                                }
                                while ( prev_permutation(a,a+5) );     //找比当前序列字典顺序小的序列(也就是前一个字典顺序序列)如果没有返回0,如果有返回1. 
                                
                            }
    printf("no solution\n");
    return 0;
} 

void BubbleSort(string &str)
{ 
    char temp;
    for(int i=0;i<str.size()-1;++i)
        for(int j=0;j<str.size()-1-i;++j)
            if(str[j]>str[j+1])
                {
                    temp=str[j];
                    str[j]=str[j+1];
                    str[j+1]=temp;
                }               
    return;
}

猜你喜欢

转载自www.cnblogs.com/wzmm/p/12442834.html