火柴棒等式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 }

 

转载于:https://www.cnblogs.com/ywjblog/p/7810574.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值