火柴棒等式

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

输入文件matches.in共一行,又一个整数n(n<=24)。

Output

输出文件matches.out共一行,表示能拼成的不同等式的数目。

Sample Input

【输入样例1】
14

【输入样例2】
18

Sample Output

【输入样例1】
2
//2个等式为0+1=1和1+0=1。

【输出样例2】
9

//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

先预处理:求0~2000数对应的火柴数目。

再循环枚举A值0~1000.

最后输出方案数

#include<iostream> using namespace std; int main() { int a[10]={6,2,5,5,4,5,6,3,7,6},ans=0,temp=0,k; int num[2016]; int n; cin>>n; num[0]=6; for (int i=1;i<=2000;i++) { k=i; while(k) { temp+=a[k%10]; k/=10; } num[i]=temp; temp=0; } for (int i=0;i<=999;i++) for (int j=0;j<=999;j++) { if (num[i]+num[j]>=n) continue; else { if (num[i+j]+num[i]+num[j]+4==n) ans=ans+1; } } cout<<ans; return 0; }

posted @ 2017-05-11 17:27  银叶草  阅读(249)  评论(0编辑  收藏  举报
Live2D