2008NOIp提高組 火柴棒等式

題目描述

給你n根火柴棍,你可以拼出多少個形如“A+B=C”的等式?等式中的A、B、C是用火柴棍拼出的整數(若該數非零,則最高位不能是0)。用火柴棍拼數字0-9的拼法如圖所示:
這裏寫圖片描述
注意:
加號與等號各自需要兩根火柴棍
如果A≠B,則A+B=C與B+A=C視爲不同的等式(A、B、C>=0)
n根火柴棍必須全部用上

輸入輸出格式

輸入格式:

輸入文件matches.in共一行,又一個整數n(n<=24)。

輸出格式:

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

輸入輸出樣例

輸入樣例#1

14

輸入樣例#2

18

輸出樣例#1

2

輸出樣例#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
暴力枚舉下相等的情況再看火柴數是否正好。
代碼如下

#include<iostream>
#include<cstdio>
#include<cstring>
#define poi int
using namespace std;
int num[10];
int getnum(int x)
{
    if(!x) return num[0];
    int ans=0;
    while(x)
    {
        ans+=num[x%10];
        x/=10;
    }
    return ans;
}
int main()
{
    int n;
    cin>>n;
    num[0] = 6;
    num[1] = 2;
    num[2] = 5;
    num[3] = 5;
    num[4] = 4;
    num[5] = 5;
    num[6] = 6;
    num[7] = 3;
    num[8] = 7;
    num[9] = 6;
    poi ans=0;
    for(int i = 0 ; i <= 725 ; i ++)
    {
        for(int j = 0 ; j <= 725 ; j ++)
        {
            poi a=i+j;
            if(getnum(i)+getnum(j)+getnum(a)+4==n)
            {
                ans++;

            }
        }
    }
    cout<<ans;
    return 0;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章