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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM