2014年第五届蓝桥杯CC++ A组国赛 —— 第二题:六角幻方

    xiaoxiao2022-07-07  207

    标题:六角幻方

    把 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; } Alex 007 认证博客专家 机器学习 NLP TensorFlow 我是 Alex 007,一个热爱计算机编程和硬件设计的小白。为啥是007呢?因为叫 Alex 的人太多了,再加上每天007的生活,Alex 007就诞生了。如果你喜欢我的文章的话,给个三连吧!
    最新回复(0)