您的位置:首页 > 其它

计蒜客 难题题库 211 火柴棍游戏

2015-08-10 10:12 253 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: