蓝桥杯:六角填数

第七题:六角填数

题目描述
如图【1.png】所示六角形中,填入1~12的数字。
使得每条直线上的数字之和都相同。
图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少?

首先:填入的数字不能相等 然后必须每一条直线 也就是六条直线的和必须相等

两种思路,全排列 或者 暴力递归9次(纯属整活)

这次就整活一次 看看暴力递归9次的感觉:

#include<iostream>
#include<math.h>
using namespace std;

int main()
{
    int a,b,c,d,e,f,g,h,i;

    for(a=2; a<=12; a++)
    {
        if(a==8||a==3)
            continue;
        for(b=2; b<=12; b++)
        {
            if(b==8||b==3||b==a)
                continue;
            for(c=2; c<=12; c++)
            {
                if(c==8||c==3||c==b||c==a)
                    continue;
                for(d=2; d<=12; d++)
                {
                    if(d==8||d==3||d==a||d==b||d==c)
                        continue;
                    for(e=2; e<=12; e++)
                    {
                        if(e==8||e==3||e==a||e==b||e==c||e==d)
                            continue;
                        for(f=2; f<=12; f++)
                        {
                            if(f==8||f==3||f==a||f==b||f==c||f==d||f==e)
                                continue;
                            for(g=2; g<=12; g++)
                            {
                                if(g==8||g==3||g==a||g==b||g==c||g==d||g==e||g==f)
                                    continue;
                                for(h=2; h<=12; h++)
                                {
                                    if(h==8||h==3||h==a||h==b||h==c||h==d||h==e||h==f||h==g)
                                        continue;
                                    for(i=2; i<=12; i++)
                                    {
                                        if(i==8||i==3||i==a||i==b||i==c||i==d||i==e||i==f||i==g||i==h)
                                            continue;

                                        int tp=8+a+b+c;
                                        int tp2=1+a+d+f;
                                        int tp3=1+b+e+i;
                                        int tp4=f+g+h+i;
                                        int tp5=8+d+g+3;
                                        int tp6=3+h+e+c;
                                        if(tp==tp2&&tp==tp3&&tp==tp4&&tp==tp5&&tp==tp6)
                                            cout<<d<<endl;

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    return 0;
}

哈哈哈哈然后附赠一下全排列的写法:转载于2014年第五届C/C++ B组蓝桥杯省赛真题_戎码一生的博客-CSDN博客

//next_permutation()
#include<iostream>
#include<algorithm>
using namespace std;
int num[9] = {2,4,5,6,7,9,10,11,12};
//判断六条直线是否等于26
bool jude()
{
	bool ans1 = (1+num[1]+num[4]+num[8] == 26);
	bool ans2 = (1+num[0]+num[3]+num[5] == 26);
	bool ans3 = (8+num[0]+num[1]+num[2] == 26);
	bool ans4 = (8+num[3]+num[6]+3 == 26);
	bool ans5 = (3+num[7]+num[4]+num[2] == 26);
	bool ans6 = (num[5]+num[6]+num[7]+num[8] == 26);
	return ans1&&ans2&&ans3&&ans4&&ans5&&ans6;
	
}
int main()
{
	
	do
	{
		if(jude())
		{
			break;
		}
	}while(next_permutation(num,num+9));
	cout<<num[3]<<endl; 
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值