学习C++从娃娃抓起!记录下洛谷C++学习和备考过程中的题目,记录每一个瞬间。
附上汇总贴:洛谷刷题C++语言 | 汇总_热爱编程的通信人的博客-CSDN博客
【题目描述】
给你 n 根火柴棍,你可以拼出多少个形如 A+B=C 的等式?等式中的 A、B、C 是用火柴棍拼出的整数(若该数非零,则最高位不能是 0)。用火柴棍拼数字 0∼9 的拼法如图所示:
注意:
- 加号与等号各自需要两根火柴棍;
- 如果 A≠B,则 A+B=C 与 B+A=C 视为不同的等式(A,B,C≥0);
- n 根火柴棍必须全部用上。
【输入】
一个整数 n(1≤n≤24)。
【输出】
一个整数,能拼成的不同等式的数目。
【输入样例】
14
【输出样例】
2
【代码详解】
#include <bits/stdc++.h>
using namespace std;
int counts[10] = {6,2,5,5,4,5,6,3,7,6};
int f(int n) {
if (n==0) return 6;
int ans=0;
while (n!=0) {
ans += counts[n%10];
n = n/10;
}
return ans;
}
int main()
{
int n, ans=0;
cin >> n;
for (int a=0; a<720; a++) {
for (int b=0; b<720; b++) {
if (f(a)+f(b)+f(a+b)+4==n) ans++;
}
}
cout << ans;
return 0;
}
【运行结果】
14
2