Referring to http://blog.csdn.net/liuchuo/article/details/51994221 , I found that the blogger of this article was a junior at the time, and I am ashamed. Although I am only an amateur typing code, the gap is huge.
1. The topic
Fill in the numbers from 1 to 12 in the hexagon as shown.
Make the sum of the numbers on each line the same.
In the picture, 3 numbers have been filled in for you. Can you calculate the number represented by the position of the asterisk?
Please submit your answer through your browser, do not fill in redundant content.
Second, the source program
#include<stdio.h> int a[13];//Each circle corresponds to an array element, a[1]-a[12] represents 12 circles, a[0] is not used int book[13];//The book array is used to mark the usage of numbers 1-12, book[0] is not used, book[1]=1, means that number 1 is used, book[1]=0 means that number 1 is not used used void dfs(int x) { if (x == 1 || x == 2 || x == 12) { dfs(x + 1); return; } if (x > 12) { int line[6]; line[0] = a[2] + a[3] + a[4] + a[5]; line[1] = a[2] + a[6] + a[9] + a[12]; line[2] = a[1] + a[3] + a[6] + a[8]; line[3] = a[1] + a[4] + a[7] + a[11]; line[4] = a[5] + a[7] + a[10] + a[12]; line[5] = a[8] + a[9] + a[10] + a[11]; for (int i = 0; i < 5; i++) { if (line[i] != line[i + 1]) { return; } } for (int i = 1; i <= 12; i++) { printf("%d ", a[i]); } printf("\n"); printf("%d\n", a[6]); } for (int i = 1; i <= 12; i++) { if (book[i] == 0) { book[i] = 1; a[x] = i; dfs(x + 1); book[i] = 0; } } } intmain() { a[1] = 1; a[2] = 8; a[12] = 3; book[1] = 1; book[8] = 1; book[3] = 1; dfs(1); return 0; }