小明帮妹妹辅导寒假作业,其中一道题是利用火柴棒拼等式,题目给你20根火柴棍,问可以拼出多少个形如 "A+B=C" 的等式?
注意:
数字0-9分别需要的火柴棒的数目为: 6,2,5,5,4,5,6,3,7,6
等式中的 A、B、C 是用火柴棍拼出的整数
若A、B、C 不为0,则最高位不能为0
加号与等号各自需要两根火柴棍
如果 A!=B,则 A+B=C与 B+A=C 视为不同的等式(A,B,C>=0)
n 根火柴棍必须全部用上
答案是一个整数,请通过浏览器直接提交该数字。
代码:
#include<iostream>
using namespace std;
int a[15]={6,2,5,5,4,5,6,3,7,6};
int f(int x)
{
if(x==0)
return a[0];
int ans=0;
while(x)
{
ans+=a[x%10];
x/=10;
}
return ans;
}
int main()
{
int cnt=0;
for(int i=0;i<=1000;i++)
for(int j=0;j<=1000;j++)
{
if(f(i)+f(j)+f(i+j)==16)
{
cnt++;
//cout<<"i="<<i<<" "<<"j="<<j<<endl;
}
}
cout<<cnt<<endl;
return 0;
}