2014年第五届蓝桥杯C/C++ A组国赛 —— 第二题:六角幻方
生活随笔
收集整理的這篇文章主要介紹了
2014年第五届蓝桥杯C/C++ A组国赛 —— 第二题:六角幻方
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
標(biāo)題:六角幻方
把 1 2 3 … 19 共19個(gè)整數(shù)排列成六角形狀,如下:
* * ** * * ** * * * ** * * * * * *要求每個(gè)直線上的數(shù)字之和必須相等。共有15條直線哦!
再給點(diǎn)線索吧!我們預(yù)先填好了2個(gè)數(shù)字,第一行的頭兩個(gè)數(shù)字是:15 13,參見圖【p1.png】,黃色一行為所求。
請(qǐng)你填寫出中間一行的5個(gè)數(shù)字。數(shù)字間用空格分開。
這是一行用空格分開的整數(shù),請(qǐng)通過瀏覽器提交答案,不要填寫任何多余的內(nèi)容(比如說明性的文字等)
Code
/*^....0^ .1 ^1^.. 011.^ 1.0^ 1 ^ ^0.11 ^ ^..^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.101 10 1.1 ^^^1111110010 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. ^.11 ^ 11 1. ^^^ ^ ^ ..^^..^ ^1 ^.^ ^^^ .0 ^.00..^ ^0 01 ^^^ .. 0..^1 .. .1 ^.^ ^^^ 1 ^ ^0001^ 1. 00 0. ^^^ ^.0 ^.1. 0^. ^.^ ^.^ ^^^ ..0.01 .^^. .^ 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^ ^ 011 0 ^. 00.0^ ^00000 1.00.1 11. 1 0 1^^0.01 ^^^ 01.^ ^ 1 1^^ ^.^1 1 0... 1 ^1 1^ ^ .01 ^ 1.. 1.1 ^0.0^ 0 1..01^^100000..0^1 1 ^ 1 ^^1111^ ^^0 ^ ^ 1 1000^.1 ^.^ . 00.. 1.1 0. 01. . 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; }總結(jié)
以上是生活随笔為你收集整理的2014年第五届蓝桥杯C/C++ A组国赛 —— 第二题:六角幻方的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2014年第五届蓝桥杯C/C++ A组国
- 下一篇: 2014年第五届蓝桥杯C/C++ A组国