zoukankan      html  css  js  c++  java
  • 2014年第五届蓝桥杯C/C++ A组国赛 —— 第二题:六角幻方

    标题:六角幻方

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

        * * *
       * * * *
      * * * * *
       * * * * 
        * * *
    

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

    再给点线索吧!我们预先填好了2个数字,第一行的头两个数字是:15 13,参见图【p1.png】,黄色一行为所求。
    在这里插入图片描述
    请你填写出中间一行的5个数字。数字间用空格分开。

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

    Code

    /*
                                    ^....0
                                   ^ .1 ^1^
                                   ..     01
                                  1.^     1.0
                                 ^ 1  ^    ^0.1
                                 1 ^        ^..^
                                 0.           ^ 0^
                                 .0            1 .^
                                 .1             ^0 .........001^
                                 .1               1. .111100....01^
                                 00                 11^        ^1. .1^
                                 1.^                              ^0  0^
                                   .^                                 ^0..1
                                   .1                                   1..^
                                 1 .0                                     ^  ^
                                  00.                                     ^^0.^
                                  ^ 0                                     ^^110.^
                              0   0 ^                                     ^^^10.01
                       ^^     10  1 1                                      ^^^1110.1
                       01     10  1.1                                      ^^^1111110
                       010    01  ^^                                        ^^^1111^1.^           ^^^
                       10  10^ 0^ 1                                            ^^111^^^0.1^       1....^
                        11     0                                               ^^11^^^ 0..  ....1^   ^ ^
                        1.     0^                                               ^11^^^ ^ 1 111^     ^ 0.
                       10   00 11                                               ^^^^^   1 0           1.
                       0^  ^0  ^0                                                ^^^^    0            0.
                       0^  1.0  .^                                               ^^^^    1 1          .0
                       ^.^  ^^  0^                             ^1                ^^^^     0.         ^.1
                       1 ^      11                             1.                ^^^     ^ ^        ..^
                      ^..^      ^1                             ^.^               ^^^       .0       ^.0
                      0..^      ^0                              01               ^^^       ..      0..^
                     1 ..        .1                             ^.^              ^^^       1 ^  ^0001
                    ^  1.        00                              0.             ^^^        ^.0 ^.1
                    . 0^.        ^.^                             ^.^            ^^^         ..0.0
                   1 .^^.         .^                  1001        ^^            ^^^         . 1^
                   . ^ ^.         11                0.    1         ^           ^^          0.
                    0  ^.          0              ^0       1                   ^^^          0.
                  0.^  1.          0^             0       .1                   ^^^          ..
                  .1   1.          00            .        .1                  ^^^           ..
                 1      1.         ^.           0         .^                  ^^            ..
                 0.     1.          .^          .         0                                  .
                 .1     1.          01          .        .                                 ^ 0
                ^.^     00          ^0          1.       ^                                 1 1
                .0      00           .            ^^^^^^                                   .
                .^      00           01                                                    ..
               1.       00           10                                                   1 ^
              ^.1       00           ^.                                            ^^^    .1
              ..        00            .1                                        1..01    ..
             1.1         00           1.                                       ..^      10
            ^ 1^         00           ^.1                                      0 1      1
            .1           00            00                                       ^  1   ^
             .           00            ^.^                                        10^  ^^
           1.1           00             00                                              10^
           ..^           1.             ^.                                               1.
          0 1            ^.              00                 00                            .^
            ^            ^.              ^ 1                00   ^0000^     ^               01
         1 0             ^.               00.0^              ^00000   1.00.1              11
         . 1              0               1^^0.01                      ^^^                01
          .^              ^                1   1^^                                       ^.^
        1 1                                                                              0.
        ..                                                                              1 ^
         1                                                                               1
       ^ ^                                                                             .0
       1                                                                             ^ 1
       ..                                                          1.1            ^0.0
      ^ 0                                                           1..01^^100000..0^
      1 1                                                            ^ 1 ^^1111^ ^^
      0 ^                                                             ^ 1      1000^
      .1                                                               ^.^     .   00
      ..                                                                1.1    0.   0
      1.                                                                  .    1.   .^
      1.                                                                 1    1.   ^0
     ^ .                                                                 ^.1 00    01
     ^.0                                                                  001.     .^
     */
    /* Procedural objectives:
    
     Variables required by the program:
    
     Procedural thinking:
    
     Functions required by the program:
    
     Determination algorithm:
    
     Determining data structure:
    
    
    */
    /* My dear Max said:
    "I like you,
    So the first bunch of sunshine I saw in the morning is you,
    The first gentle breeze that passed through my ear is you,
    The first star I see is also you.
    The world I see is all your shadow."
    
    FIGHTING FOR OUR FUTURE!!!
    */
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    int num[20];
    int ans[20];
    bool vis[20];
    
    void dfs(int index){
    	if(index==4&&(ans[1]+ans[2]+ans[3]!=38))
    		return;
    	if(index==8&&(ans[4]+ans[5]+ans[6]+ans[7]!=38))
    		return;
    	if(index==9&&(15+ans[4]+ans[8]!=38))
    		return;
    	if(index==13&&(ans[8]+ans[9]+ans[10]+ans[11]+ans[12]!=38))
    		return;
    	if(index==14&&((13+ans[5]+ans[9]+ans[13]!=38)||(ans[3]+ans[7]+ans[12]!=38)))
    		return;
    	if(index==17&&((ans[13]+ans[14]+ans[15]+ans[16]!=38)||(13+ans[6]+ans[11]+ans[16]!=38)))
    		return;
    	if(index==18&&((ans[3]+ans[6]+ans[10]+ans[14]+ans[17]!=38)||(ans[3]+ans[6]+ans[10]+ans[14]+ans[17]!=38)))
    		return;
    	if(index==19&&(ans[7]+ans[11]+ans[15]+ans[18]!=38||ans[4]+ans[9]+ans[14]+ans[18]!=38))
    		return;
    	
    	if(index==20){
    		if(ans[17]+ans[18]+ans[19]==38){
    			cout<<ans[8]<<' '<<ans[9]<<' '<<ans[10]<<' '<<ans[11]<<' '<<ans[12]<<endl;
    			return;
    		}
    	}
    	
    	for(int i=1;i<20;i++){
    		if(!vis[i]){
    			vis[i]=true;
    			ans[index]=num[i];
    			dfs(index+1);
    			vis[i]=false;
    		}
    	}
    }
    
    int main(){
    	ans[1]=15;
    	ans[2]=13;
    	vis[13]=vis[15]=true;
    	for(int i=1;i<20;i++) num[i]=i;
    	dfs(3);
    	return 0;
    } 
    
  • 相关阅读:
    谢尔排序(缩小增量排序)
    折半插入排序
    插入排序
    选择排序
    冒泡排序
    ul+js模拟select+改进
    ul+js模拟select
    前端面试题
    js数组去重的三种常用方法总结
    使用PHP实现文件上传和多文件上传
  • 原文地址:https://www.cnblogs.com/AlexKing007/p/12338258.html
Copyright © 2011-2022 走看看