1168 火柴棒等式

1168 火柴棒等式

链接:http://codevs.cn/problem/1168/

 

2008年NOIP全国联赛提高组

 时间限制: 1 s
 空间限制: 128000 KB
 
 
 
题目描述 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 Description

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

输出描述 Output Description

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

样例输入 Sample Input

样例1:

14

 

样例2:

18

样例输出 Sample Output

样例1:

2

 

样例2:

9

数据范围及提示 Data Size & Hint

【输入输出样例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

#include<iostream>
using namespace std;
int a[11]={6,2,5,5,4,5,6,3,7,6};
int h(int x){
    int ans=0;
    if(x==0)ans=6;
    while(x){
        ans+=a[x%10];
        x/=10;
    }
    return ans;
}
int main(){
    int n,cnt=0;cin>>n;
    n-=4;
    if(n<=0)cout<<cnt<<endl;
    for(int i=0;i<=1000;i++){
        int x=h(i);
        if(x>n);
        else for(int j=0;j<=1000;j++){
            int y=h(j);
            if(y>n);
            else if(x+y<n){
                if(h(i+j)==n-x-y){
                    cnt++;
                }
            }
            
        }
        
    }    
    cout<<cnt<<endl;    
            
}

 

posted @ 2017-08-29 11:46  Ed_Sheeran  阅读(182)  评论(0编辑  收藏  举报