火柴棒等式c++

时间:2023-03-09 01:14:43
火柴棒等式c++

火柴棒等式c++

先建立一个sum数组,打表存入1—9每个数字需要的火柴棒数,然后手动二重循环0—1000的所有数字,写一个int型函数用来计算每个数字需要多少根火柴棒(当前数字%10后在sum数组的下标),然后,最后返回

如果数字A+数字B+数字C+4(等号和加号)==总火柴棒数,计数器++;

 #include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <ctime>
using namespace std;
int sum[] = {, , , , , , , , , };
int n,t = ;
int work(int x) {
int ans = , k;
if(x == ) return sum[];
else {
while(x != ) {
k = x % ;
x /= ;
ans += sum[k];
}
return ans;
}
}
int main() {
cin >> n;
for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++) {
if(work(i) + work(j) + work(i + j) + == n)
t++;
}
cout << t << '\n';
return ;
}