火柴棍的等式 算法


这里有个地方有点坑,“若该数非零,则最高位不能是0”(若该数是零,那么最高位可以为0?    但上传结果后表示 ,像00是不允许的)

算法思路是: 回溯求的等式序列,回溯过程用“所用的火柴棍小于等于总和”进行剪枝

得到可能的序列后,分割加数与和所占的序列,再判断和等式是否成立

/*
给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?
等式中的A、B、C是用火柴棍拼出的整数(若该数非零,则最高位不能是0)。用火柴棍拼数字0-9的拼法如图所示: 
注意: 

1. 加号与等号各自需要两根火柴棍 .
2. 如果A≠B,则A+B=C与B+A=C视为不同的等式(A、B、C>=0). 
3. n根火柴棍必须全部用上 .
当n=14的时候,只能拼成2种不同的等式(2个等式为0+1=1和1+0=1。)

请问当n=20的时候,能拼成多少种不同的等式? 

0 6
1 2
2 5
3 5
4 4
5 5
6 6
7 3
8 7
9 6
*/


// ceshiyongde.cpp : 定义控制台应用程序的入口点。

//


#include "stdafx.h"
#include <iostream>

using namespace std;


int ProductDengShi(int * a, int maxlen)
{
int t;
int count=0;
int i,numOneEnd, numTwoEnd;//
int jiaShuOne, jiaShuTwo,he;
if(4 == maxlen)
t=0;
for(numOneEnd=1; numOneEnd<=maxlen/2; numOneEnd++)//[0,numOneEnd)
{
for(numTwoEnd=numOneEnd+1; numTwoEnd<maxlen; numTwoEnd++)//[numOneEnd, numTwoEnd)
{
if( ( (maxlen-numTwoEnd) >= (numTwoEnd-numOneEnd) )//和的长度大于等于加数的长度
 ||( (maxlen-numTwoEnd) >= (numOneEnd) )
)
{
jiaShuOne=jiaShuTwo=he=0;
for(i=0; i<numOneEnd; i++)
{
jiaShuOne=jiaShuOne*10+a[i];
}
if( ((0 != jiaShuOne) && (0 == a[0]))  ||  ( (0 == jiaShuOne) && (numOneEnd-0)>1) )
continue;


for(i=numOneEnd; i<numTwoEnd; i++)
{
jiaShuTwo=jiaShuTwo*10+a[i];
}
if(  (0 != jiaShuTwo) && (0 == a[numOneEnd]) ||  ( (0 == jiaShuTwo) && (numTwoEnd-numOneEnd)>1)  )
continue;




for(i=numTwoEnd; i<maxlen; i++)
{
he=he*10+a[i];
}
if( (0 != he) && (0 == a[numTwoEnd]) ||  ( (0 == he) && (maxlen-numTwoEnd)>1))
continue;


if(jiaShuOne+jiaShuTwo == he)
{
cout<<jiaShuOne<<"+"<<jiaShuTwo<<"="<<he<<"("<<maxlen<<")"<<endl;
count++;
}
}
else
break;


}
}
return count;
}


bool IsDengYuJiaQuanCheck(int * jiaQuanArr, int keYongN,int * a, int maxlen)
{
int i,sum=0;


for(i=0; i<maxlen; i++)
{
sum+=jiaQuanArr[ a[i] ];
}



if(sum == keYongN)
return true;
else
return false;
}


bool IsPassXiaoYuDengYuJiaQuanCheck(int * jiaQuanArr, int keYongN,int * a, int sub, int shiTanZhi)
{
int i,sum=0;


for(i=0; i<sub; i++)
{
sum+=jiaQuanArr[ a[i] ];
}


sum+=jiaQuanArr[ shiTanZhi ];




if(sum <= keYongN)
return true;
else
return false;
}




void Product(int * jiaQuanArr, int keYongN)
{
int count=0,t=0;
int a[10];
int sub,shiTanZhi;
int maxlen=3;


for(maxlen=3; maxlen<keYongN/2; maxlen++)
{
a[0]=0;


sub=1;
shiTanZhi=0;


while(true)
{
if(sub<0)//回溯完成
break;


if(sub >= maxlen)//产生可能解
{


if(IsDengYuJiaQuanCheck(jiaQuanArr, keYongN, a, maxlen))
{
count+=ProductDengShi(a, maxlen);
}




sub--;
shiTanZhi=a[sub]+1;
}


if(shiTanZhi > 9)//试探值超过上限
{
sub--;
shiTanZhi=a[sub]+1;
}
else
{


if( IsPassXiaoYuDengYuJiaQuanCheck(jiaQuanArr, keYongN, a, sub, shiTanZhi) )//通过加权值检测
{
a[sub]=shiTanZhi;
sub++;
shiTanZhi=0;
}
else
{
shiTanZhi++;
}


}
}
}


cout<<"拼法有"<<count<<"种"<<endl;
}


int _tmain(int argc, _TCHAR* argv[])




int jiaQuanArr[10]={6,2,5,5,4,5,6,3,7,6};
int const n=20;
int const keYongN=n - 4;//减掉 加号和等号 消耗的火柴棍


Product(jiaQuanArr, keYongN);








getchar(); 
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值