描述
给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A、B、C是用火柴棍拼出的整数(若该数非零,则最高位不能是0)。用火柴棍拼数字0-9的拼法如图所示:
注意:
1. 加号与等号各自需要两根火柴棍
2. A+B=C与B+A=C视为不同的等式(A、B、C>=0)
3. n根火柴棍必须全部用上
输入
输入共一行,又一个整数n(n<=24)。
输出
输出共一行,表示能拼成的不同等式的数目。
样例输入
14
样例输出
2
提示
2个等式为0+1=1和1+0=1。
A、B、C的值可能是多位数
这题没什么方法,只能用枚举了...
#include<cstdio>
#include<iostream>
using namespace std;
int sd[11]={6,2,5,5,4,5,6,3,7,6};
int fun(int a)//计算每个数字火柴数量
{
int ans=0;
if(a==0)return sd[0];
while(a>0)
{
ans+=sd[a%10];
a=a/10;
}
return ans;
}
int main()
{
int n,i,j;
while(cin>>n)
{
int m=0;
for(i=0;i<1000;i++)//枚举
for(j=0;j<1000;j++)
{
int a=fun(i),b=fun(j),c=fun(i+j);//计算每个地方的火柴数量
if((c+a+b)==n-4)//因为'+'和'='号占据了四个火柴,所以要减去
m++;
}
cout<<m<<endl;
}
return 0;
}