蓝桥杯:六角填数问题

标题:六角填数
如图【1.png】所示六角形中,填入1~12的数字。
使得每条直线上的数字之和都相同。
图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少?
蓝桥杯:六角填数问题_第1张图片

答案:9 10 6 5 4 11 12 2 7

蓝桥杯:六角填数问题_第2张图片
代码:
蓝桥杯:六角填数问题_第3张图片

public class LiuJiao {
    public static void main(String[] args) {
        for (int c21 = 2; c21 < 12; c21++) {//B
            if(c21==3||c21==8) continue;
            for (int c22 = 2; c22 < 12; c22++) {//C
                if (c22==3||c22==8||c21==c22) continue;
                for (int c31 = 2; c31 < 12; c31++) {//D
                    if (c31==3||c31==8||c21==c31||c22==c31) continue;
                    for (int c32 = 2; c32 < 12; c32++) {//E
                        if (c32==3||c32==8||c21==c32||c22==c32||c31==c32) continue;
                        for (int c33 = 2; c33 < 12; c33++) {//F
                            if (c33==3||c33==8||c21==c33||c22==c33||c31==c33||c32==c33) continue;
                            for (int c41 = 2; c41 <= 12; c41++) {//G
                                if (c41==3||c41==8||c21==c41||c22==c41||c31==c41||c32==c41||c33==c41) continue;
                                for (int c42 = 2; c42 <= 12; c42++) {//H
                                    if (c42==3||c42==8||c21==c42||c22==c42||c31==c42||c32==c42||c33==c42||c41==c42) continue;
                                    for (int c43 = 2; c43 <= 12; c43++) {//I
                                        if (c43==3||c43==8||c21==c43||c22==c43||c31==c43||c32==c43||c33==c43||c41==c43||c42==c43) continue;
                                        for (int c44 = 2; c44 <= 12; c44++) {//J
                                            if (c44==3||c44==8||c21==c44||c22==c44||c31==c44||c32==c44||c33==c44||c41==c44||c42==c44||c43==c44) continue;
                                            if (1+c21+c22+c31==c31+c32+c33+c41) {
                                                if (1+c21+c22+c31==1+c43+c42+c41) {
                                                    if (1+c21+c22+c31==8+c22+c32+3) {
                                                        if (1+c21+c22+c31==8+c21+c43+c44) {
                                                            if (1+c21+c22+c31==c44+c42+c33+3) {
                                                                System.out.println(c21+" "+c22+" "+c31+" "+c32+" "+c33+" "+c41+" "+c42+" "+c43+" "+c44);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

你可能感兴趣的:(算法)