天天看點

計蒜客 難題題庫 211 火柴棍遊戲

  •  32次 
  •  9.37% 
  •  1000ms 
  •  65536K

給你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解釋】

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

輸入:

【輸入樣例1】 
14
【輸入樣例2】 
18      

輸出:

【輸出樣例1】 
2
【輸出樣例2】 
9      
#include<iostream>
using namespace std;

const int a[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};

int count(int n){
    int weight = 10;
    while(weight <= n){
        weight *= 10;
    }
    weight /= 10;
    int res = 0;
    while(weight){
        res += a[n / weight];
        n %= weight;
        weight /= 10;
    }
    return res;
}

int main(){
    int n, res = 0;
    cin >> n;
    n -= 4;
    for(int i = 0; i < 1000; ++i){
        for(int j = 0; j < 1000; ++j){
            if(count(i) + count(j) + count(i + j) == n){
                ++res;
            }
        }
    }
    cout << res << endl;
}