火柴棒等式c++

 

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

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

 1 #include <iostream>
 2 #include <iomanip>
 3 #include <cstdio>
 4 #include <cmath>
 5 #include <cstring>
 6 #include <algorithm>
 7 #include <ctime>
 8 using namespace std;
 9 int sum[11] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6};
10 int n,t = 0;
11 int work(int x) {
12     int ans = 0, k;
13     if(x == 0) return sum[0];
14     else {
15         while(x != 0) {
16             k = x % 10;
17             x /= 10;
18             ans += sum[k];
19         }
20         return ans;
21     }
22 }
23 int main() {
24     cin >> n;
25     for(int i = 0; i <= 1000; i++)
26         for(int j = 0; j <= 1000; j++) {
27             if(work(i) + work(j) + work(i + j) + 4 == n)
28                 t++;
29         }
30     cout << t << '\n';
31     return 0;
32 }

 

相关文章:

  • 2021-10-19
  • 2021-11-19
  • 2021-08-21
  • 2021-09-07
  • 2021-12-20
  • 2021-09-17
  • 2021-09-13
  • 2021-09-02
猜你喜欢
  • 2019-07-29
  • 2021-11-27
  • 2021-12-06
  • 2020-10-26
  • 2021-10-14
  • 2021-12-05
  • 2021-12-05
  • 2021-09-08
相关资源
相似解决方案