TYVJ P1012 火柴棒等式 Label:枚举

背景

NOIP2008年提高组第二题

描述

给你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根火柴棍必须全部用上

输入格式

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

输出格式

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

测试样例1

输入

【输入样例1】 
14 
【输入样例2】 
18

输出

【输出样例1】 

【输出样例2】 
9

备注

【输入输出样例1解释】
2个等式为0+1=1和1+0=1。
【输入输出样例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

代码

 1 #include<iostream>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<cstdio>
 5 using namespace std;
 6 int a[10]={6,2,5,5,4,5,6,3,7,6},sum[1005],
 7     N,M,ans;
 8 int get(int i){
 9     if(i==0) return 6;
10     if(sum[i]>0) return sum[i];
11     int pos=0;
12     while(i>0){
13         pos+=a[i%10];
14         i/=10;
15     }
16     return sum[i]=pos;
17 }
18 int main(){
19 //  freopen("01.txt","r",stdin);
20     scanf("%d",&N);
21     N-=4;
22     for(int i=0;i<=1000;i++){
23         if(get(i)>N) continue;
24         for(int j=0;j<=1000;j++){
25             if(get(i)+get(j)>N) continue;
26             if(get(i)+get(j)+get(i+j)==N){
27                 ++ans;
28             }
29         }
30     }
31     printf("%d
",ans);
32     return 0;
33 }

枚举到1000,加了点记忆化,(题解说枚举到800就好)

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!
原文地址:https://www.cnblogs.com/radiumlrb/p/5801250.html