Description |
给你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根火柴棍必须全部用上
|
Input |
多行,每行一个整数n(n<=24)。 |
Output |
每个一行,表示能拼成的不同等式的数目。 |
Sample Input |
14
18
|
Sample Output |
2
9
|
Hint |
N=14时有两个等式:
0+1=1
1+0=1。
N=18时9个等式为:
0+4=4
0+11=11
1+10=11
2+2=4
2+7=9
4+0=4
7+2=9
10+1=11
11+0=11
|
枚举所有可能
#include <iostream>
using namespace std;
int amount[1201]={6,2,5,5,4,5,6,3,7,6}; //0-9这些数字要用到的火柴棒数
int main()
{
int n,sum=0,i,j;
for(i=10;i<=1200;i++) //打表另外得到10-1200这些数要用到的火柴棒数
{
int t=i;
while(t>0)
{
amount[i]=amount[i]+amount[t%10];
t=t/10;
}
}
while(cin>>n)
{
n=n-4; //“+”与"="共需要4个火柴棒
sum=0;
for(i=0;i<=1200;i++)
for(j=0;j<=1200;j++)
if(i+j<=1200&&amount[i]+amount[j]+amount[i+j]==n)
sum++;
cout<<sum<<endl;
}
return 0;
}