程序说明
估计最大的数不超过1111,因为1111+1=1112需要的火柴数会大于24。先计算出1111以内所有的数所需要的火柴数,然后循环遍历。
代码如下:
#include <iostream>
using namespace std;
int a[2224];
int main() {
int n, count = 0;
cin>>n;
int s[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
for(int i = 0; i <= 2224; i++) {
int j = i;
if(j == 0) {
a[j] = 6;
}
while(j >= 1) {
a[i] = a[i] + s[j%10];
j /= 10;
}
}
for(int i = 0; i <= 1111; i++)
for(int j = 0; j <= 1111; j++) {
if(a[i] + a[j] + a[i+j] + 4 == n) {
count++;
}
}
cout<<count;
return 0;
}