标题:六角填数
如图【1.png】
所示六角形中,填入1~12的数字。 使得每条直线上的数字之和都相同。
图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少?
请通过浏览器提交答案,不要填写多余的内容。简单的枚举排列,只要提前将12个结点标号,来判断六个线段总和是否相等。
# include <iostream>
# include <algorithm>
# include <cstring>
using namespace std;
int a[12]={1,8,2,4,5,6,7,9,10,11,12,3};
int t[6];
void display(){
int i;
for(i=0;i<12;i++){
cout<<a[i]<<" ";
}
cout<<endl;
}
bool judge(){
int i;
t[0]=a[0]+a[2]+a[5]+a[7];
t[1]=a[7]+a[8]+a[9]+a[10];
t[2]=a[0]+a[3]+a[6]+a[10];
t[3]=a[1]+a[2]+a[3]+a[4];
t[4]=a[1]+a[5]+a[8]+a[11];
t[5]=a[5]+a[6]+a[9]+a[11];
for(i=0;i<4;i++){
if(t[i]!=t[i+1])return false;
}
return true;
}
int main(){
while(next_permutation(a+2,a+11)){
if(judge()){
display();
cout<<a[5]<<endl;
}
}
return 0;
}
这道题目的话因为是简答题做起来非常快,全排列 完全不用多考虑