提交时间:2026-02-05 15:03:02

运行 ID: 83996

#include <iostream> #include <string> using namespace std; // 预定义0-9每个数字需要的火柴棍数量 int match_num[10] = {6, 2, 5, 5, 4, 5, 6, 3, 7, 6}; // 计算一个整数需要的火柴棍数量,同时检查是否有前置0(非零数) int count_matches(int num, bool& valid) { valid = true; // 处理0的情况 if (num == 0) { return match_num[0]; } int cnt = 0; // 检查是否有前置0(非零数) string s = to_string(num); if (s[0] == '0') { valid = false; return 0; } // 计算每个数字的火柴棍数 while (num > 0) { int digit = num % 10; cnt += match_num[digit]; num /= 10; } return cnt; } int main() { int n; cin >> n; // 加号+等号需要4根火柴,剩余火柴数不足则直接返回0 int remain = n - 4; if (remain < 3) { // 至少需要A(1根)+B(1根)+C(1根),但实际最小是0+0=0(6+6+6=18根) cout << 0 << endl; return 0; } int total = 0; // 枚举A和B的可能值,上限设为2000足够覆盖n≤24的情况 for (int A = 0; A <= 2000; ++A) { bool valid_A, valid_B, valid_C; int a_matches = count_matches(A, valid_A); if (!valid_A || a_matches >= remain) { continue; } for (int B = 0; B <= 2000; ++B) { int b_matches = count_matches(B, valid_B); if (!valid_B || (a_matches + b_matches) >= remain) { continue; } int C = A + B; int c_matches = count_matches(C, valid_C); if (!valid_C) { continue; } // 验证火柴棍总数是否刚好等于剩余数量 if (a_matches + b_matches + c_matches == remain) { total++; } } } cout << total << endl; return 0; }