杭电ACM OJ 1015 Safecracker 优雅的5重for循环轻松解决v - w^2 + x^3 - y^4 + z^5 = target

本文介绍了一种解决Safecracker问题的方法,这是一种涉及数学运算和字符组合的复杂挑战。通过五重循环算法,寻找特定字符组合以满足预设等式。

Safecracker

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15408    Accepted Submission(s): 8118


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 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
翻译,输入一个字符串,比如ABCDEFGHIJKL,顺序任意,A代表1,B代表2.。。以此类推。
希望可以从中找到5个字符,满足v - w^2 + x^3 - y^4 + z^5 = target ,target也是你给定的,如第一个例子的1.
其中v,w,x,y,z随便放你这个字符串里的那个字符都可以,但是一个字符不可以用两次。
如果找不到这个结果,就no solution,如果只有一个,就要输出这5个字符。如果有多个,按首字母最大的情况来。
比如 找到两个LKEBA ABCDE(当然abcde是我乱给的,应该不对)符合条件的,那么就取L开头的那个输出。
核心:5重for循环
public class Safecracker1015 {

    private static char[] str = new char[25];
    private static int value;

    private static int a;
    private static int b;
    private static int c;
    private static int d;
    private static int e;

    public static void main(final String[] args) throws Exception {

        initData();

        calculate(str, value);

        char mA = (char) (a + 64);
        char mB = (char) (b + 64);
        char mC = (char) (c + 64);
        char mD = (char) (d + 64);
        char mE = (char) (e + 64);

        System.out.println(mA + " " + mB + " " + mC + " " + mD + " " + mE);
    }

    private static void initData() {

        initStr('A', 'B', 'C', 'D', 'E', 'F','G','H','J','K','L');

        sortStr(str);

        value = 1;
    }


    private static void initStr(char...chars) {
        int size = chars.length;
        for (int i = 0; i < size; i ++) {
            str[i] = chars[i];
        }
    }

    private static void sortStr(char[] chars) {//由于首字母要取最大,所以要降序排列
        int length = chars.length;
        char temp;
        for (int i = 0; i < length - 1; i ++) {
            for (int j = 0; j < length - i - 1; j ++) {
                if (chars[j] < chars[j + 1]) {
                    temp = chars[j];
                    chars[j] = chars[j + 1];
                    chars[j + 1] = temp;
                }
            }
        }
    }

    private static void calculate(char[] str, int value) {
        int length = str.length;

        a = 0;
        b = 0;
        c = 0;
        d = 0;
        e = 0;

        for (int i = 0; i < length; i ++) {

            for (int j = 0; j < length; j ++) {
                if (j == i) continue;

                for (int k = 0; k < length; k ++) {
                    if (k == j || k == i) continue;

                    for (int l = 0; l < length; l ++) {
                        if (l == k || l == j || l ==i) continue;

                        for (int m = 0; m < length; m ++) {
                            if (m == l || m == k || m == j || m == i) continue;

                            a = str[i] - 'A' + 1;
                            b = str[j] - 'A' + 1;
                            c = str[k] - 'A' + 1;
                            d = str[l] - 'A' + 1;
                            e = str[m] - 'A' + 1;

                            if (a - b * b + c * c * c - d * d * d * d +
                                    e * e * e * e * e == value) {
                                return;
                            }
                        }
                    }
                }
            }
        }
    }
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值