HDoj 1015 Safecracker

The Description Problem
=== Op Tech Briefing, 2002/11/02 06:42 CST ===
"Locked in at The Item IS A Safe Klein (Klein safe) behind in Painting at The SECOND, A-Floor Library.
Klein Safes are extremely rare (rare);. most of them, along with Klein and his factory ( along with Klein and his function), were Destroyed in World War II
fortunately Old Brumbaugh (Brembo, names) from research knew Klein's Secrets and wrote Them Down the before of He died.
a Klein Safe has TWO Distinguishing features (features): a combination lock (combination lock , combination lock) that uses letters instead of numbers, and an engraved quotation ( deep deep into the quotations)
ON Door the.
a Klein and Quotation Always the contains BETWEEN Twelve Five DISTINCT uPPERCASE (capital letters) letters, usually at the beginning of sentences, and mentions ( referred to) 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

 

Topic resolve this problem mainly on the whole arrangement, learned specific points are:

1 how a string in the descending order ABCD no function, a sort their own handwriting

2 How to use multiple combinations for loop iterates

3 How to exit function plus a return statement used in the loop for this question, there are more ways in the collection in

How 4 full array of stl using c ++ library, there are two functions to find a smaller order than the current sequence under a full array b dictionary to find a full array larger than the current sequence under, remember to use do while loop, otherwise the first sequences will not be displayed.

5 with reference to the function parameters, reference characters to be added after the variable type declaration

 

C language code as follows:

#include <stdio.h> 
#include <the iostream> 
#include < String > 
#include <algorithm>
 the using  namespace STD;
 char A [ . 5 ];
 int Solution ( Long  Long , String );
 void the BubbleSort ( String &);     // with reference to the function parameter declaration 
int main () 
{ 
    int  Long  Long n-= 0 ;
     String STR = "" ; 

    the while (CIN >> n-)  
    {
        CIN>> STR; 
        
        // COUT << STR; 
        IF (n-== 0 && STR == " the END " )
             BREAK ;
         the else 
            the BubbleSort (STR);     // this strings alphabetically descending order 
            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) );     //Get smaller than the current sequence sequence sequence dictionaries (dictionary is sequentially before a sequence) returns 0 if not, if there returns 1. 
                                
                            } 
    the printf ( " NO Solution \ n- " );
     return  0 ; 
} 

void the 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 [s]; 
                    str [s] = str [j + 1 ]; 
                    str [j + 1 ] = temp; 
                }                
    Return ; 
}

 

Guess you like

Origin www.cnblogs.com/wzmm/p/12442834.html