• 六角幻方


    标题:六角幻方

        把 1 2 3 ... 19 共19个整数排列成六角形状,如下:

        * * *

       * * * *

      * * * * *

       * * * *

        * * *

        要求每个直线上的数字之和必须相等。共有15条直线哦!

        再给点线索吧!我们预先填好了2个数字,第一行的头两个数字是:15 13,参见图【p1.png】,黄色一行为所求。

        请你填写出中间一行的5个数字。数字间用空格分开。

        这是一行用空格分开的整数,请通过浏览器提交答案,不要填写任何多余的内容(比如说明性的文字等)


    肯定不能直接全排列,17个数的全排列时间过长。需要进行中间判断

    填完第8个,第9个,第13个,第16个,以及第20个越早判断复杂度越低。

    结果:9 6 5 2 16

     1 import java.util.Arrays;
     2 import java.util.Scanner;
     3 import java.util.Stack;
     4 
     5 class Main{
     6     static int[] a;
     7     static boolean[] b;
     8     static int n = 19;
     9     public static void main(String[] args){
    10         a = new int[n+1];
    11         b = new boolean[n+1];
    12         a[1] = 15;
    13         a[2] = 13;
    14         f(3);
    15     }
    16 
    17     public static void f(int i){
    18         if(i==8){
    19             int c = a[1]+a[2]+a[3];
    20             int c7 = a[4]+a[5]+a[6]+a[7];
    21             if(c!=c7){
    22                 return;
    23             }
    24         }
    25         if(i==9){
    26             int c = a[1]+a[2]+a[3];
    27             int c2 = a[1]+a[4]+a[8];
    28             if(c!=c2){
    29                 return;
    30             }
    31         }
    32         if(i==13){
    33             int c = a[1]+a[2]+a[3];
    34             int c8 = a[8]+a[9]+a[10]+a[11]+a[12];
    35             int c6 = a[12]+a[7]+a[3];
    36             if(c!=c8||c!=c6){
    37                 return;
    38             }
    39         }
    40     
    41         if(i==20){
    42             if(f1()){
    43                 System.out.println(a[8]+" "+a[9]+" "+a[10]+" "+a[11]+" "+a[12]);
    44             }
    45             return;
    46         }
    47         
    48         for(int j=1;j<=19;j++){
    49             if(j==15||j==13) continue;
    50             if(!b[j]){
    51                 b[j] = true;
    52                 a[i] = j;
    53                 f(i+1);
    54                 b[j] = false;
    55             }
    56         }
    57     }
    58      
    59     public static boolean f1(){
    60         int c = a[1]+a[2]+a[3];
    61         int c2 = a[1]+a[4]+a[8];
    62         int c3 = a[8]+a[13]+a[17];
    63         int c4 = a[17]+a[18]+a[19];
    64         int c5 = a[19]+a[16]+a[12];
    65         int c6 = a[12]+a[7]+a[3];
    66         int c7 = a[4]+a[5]+a[6]+a[7];
    67         int c8 = a[8]+a[9]+a[10]+a[11]+a[12];
    68         int c9 = a[13]+a[14]+a[15]+a[16];
    69         int c10 = a[4]+a[9]+a[14]+a[18];
    70         int c11 = a[1]+a[5]+a[10]+a[15]+a[19];
    71         int c12 = a[2]+a[6]+a[11]+a[16];
    72         int c13 = a[2]+a[5]+a[9]+a[13];
    73         int c14 = a[3]+a[6]+a[10]+a[14]+a[17];
    74         int c15 = a[7]+a[11]+a[15]+a[18];
    75         if(c2==c&&c3==c&&c4==c&&c5==c&&c6==c&&c7==c&&c8==c&&c9==c&&c10==c&&c11==c&&c12==c&&c13==c&&c14==c&&c15==c){
    76             return true;
    77         }else{
    78             return false;
    79         }
    80         
    81     }
    82 }
  • 相关阅读:
    python全栈闯关--16-匿名函数
    python全栈闯关--15-内置函数
    python全栈闯关--14-生成器进阶
    示例库
    MySQL的远程连接
    前后端传输编码方式
    后端接收前端时间参数
    控制器接参的空值问题
    MyBatis模糊查询的几种方式
    MySQL常用函数
  • 原文地址:https://www.cnblogs.com/lolybj/p/6720073.html
Copyright © 2020-2023  润新知