游客 Signup | Login
中文 | En

3543 - 六角填数(2014年蓝桥杯省赛第7题)

 如图所示六角形中,填入1~12的数字。

使得每条直线上的数字之和都相同。

    图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少?

<img style="height:189px;width:181px;" alt="" src="http://tk.hustoj.com:80/attached/image/20141219/20141219010328_81520.png" width="291" height="298" />

Input

Output

Examples

Input


                

Output


                

Solution C

#include<stdio.h>
#define N 15 //使用define不需要加分号
int a[N];
int book[N];
void dfs(int step)
{
  int i;
  if(step == 1 || step == 2 || step == 12)
        {  
        dfs(step+1);  
        return ; 
}
  if(step==13)
  {
  int t[6];
     t[0] = a[1] + a[3] + a[6] + a[8];  
        t[1] = a[1] + a[4] + a[7] + a[11];  
        t[2] = a[2] + a[3] + a[4] + a[5];  
        t[3] = a[2] + a[6] + a[9] + a[12];  
        t[4] = a[8] + a[9] + a[10] + a[11];  
        t[5] = a[12] + a[10] + a[7] + a[5];
        for(i=1;i<6;i++)
        {
        if(t[i]!=t[i-1])
        return ;
}
printf("%d ",a[6]);
return ;
 } 
for(i=1;i<=12;++i)
{
if(book[i]==0)
{
a[step]=i;
book[i]=1;
dfs(step+1);
book[i]=0;
} 
} 
 } 
int main()
{
book[1]=1;
 a[1]=1;
  book[8]=1;
 a[2]=8;
 book[3]=1;
 a[12]=3;
dfs(1);
 } 

Solution C++

#include <algorithm>
#include <iostream>
using namespace std;
int p[9]={2,4,5,6,7,9,10,11,12};
int main()
{
	do
	{
		int a=8+p[0]+p[1]+p[2];
		int b=8+p[3]+p[6]+3;
		int c=3+p[2]+p[4]+p[7];
		int d=1+p[0]+p[3]+p[5];
		int e=1+p[1]+p[4]+p[8];
		int f=p[5]+p[6]+p[7]+p[8];
		if(a==b&&b==c&&c==d&&d==e&&e==f)
			break;
	}
	while(next_permutation(p,p+9));
	cout<<p[3];
	return 0;
}

Time Limit 1 second
Memory Limit 128 MB
Discuss Stats
上一题 下一题