山东济南彤昌机械科技有限公司 山东济南江鹏工贸游有限公司

NOIP2008 火柴棒等式

1.      火柴棒等式

(matches.pas/c/cpp)

 

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

 

注意:

  1. 加号与等号各自需要两根火柴棍
  2. 如果 AB,则 A+B=C 与 B+A=C 视为不同的等式(A、B、C>=0)
  3. n 根火柴棍必须全部用上

 

【输入】

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

 

【输出】

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

 

【输入输出样例1】

matches.in

matches.out

14

2

 

【输入输出样例1解释】

2 个等式为 0+1=1 和 1+0=1。

 

【输入输出样例2】

 

matches.in

matches.out

18

9

 

【输入输出样例2解释】

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

 

【思路】

  枚举+判断。

  题目数据范围很小,可以知道等式中的一个数最大不超过1111,枚举ij判断k=i+j与ij是否满足火柴棍数为n即可。

 

【代码】

 1 #include<iostream>
 2 using namespace std;
 3 
 4 const int maxn = 1111;
 5 const int matches[]={6,2,5,5,4,5,6,3,7,6};
 6 int ans=0,n;
 7 int _sum[maxn+5];
 8 
 9 inline int get_sum(int x) {
10     if(x==0) return matches[0];
11     int sum=0;
12     while(x) {
13         sum+=matches[x%10];
14         x/=10;
15     }
16     return sum;
17 }
18 
19 int main () {
20     ios::sync_with_stdio(false);
21     cin>>n;
22     n-=4;
23     for(int i=0;i<=maxn;i++) for(int j=0;j<=maxn;j++) {
24         int k=i+j;
25         if(get_sum(i)+get_sum(j)+get_sum(k)== n) {
26             ans++;
27         }
28     }
29     cout<<ans;    
30     return 0;
31 }

 

posted on 2015-10-07 20:52  hahalidaxin  阅读(355)  评论(0编辑  收藏  举报