栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

蓝桥杯:六角填数

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

蓝桥杯:六角填数

第七题:六角填数

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

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

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

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

#include
#include
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< 

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

//next_permutation()
#include
#include
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< 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/769217.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号