You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include<bits/stdc++.h>
using namespace std;
/*
vs C++ scanf 不安全
项目-》属性-》 c/c++-》预处理器-》预处理器定义-》加入下面这句:
_CRT_SECURE_NO_DEPRECATE
*/
/*
* 现有m根火柴m<=24希望拼出A+B=C的等式。等式中的A、B、C均是由火柴棍拼出来的
若该数非零则第一位不能为0加号与等号各自需要两根火柴
如果A=B则A+B=C与B+A=C视为不同的等式A、B、C都大于0所有火柴棍均大于0
*/
int fun(int x) {
int num = 0;
int f[] = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6 };//初始化数组,记录每个数字所需要的火柴个数
while (x / 10 != 0) { //如果x商10不等于0说明至少有两位数
//将末尾的数所需要的火柴棍个数加到num中
num += f[x % 10];
x = x / 10;
}
num += f[x];
return num;
}
int main() {
int a = 0;
int b = 0;
int c = 0;
int sum = 0;
int m = 0;
//输入需要多少根火柴
cin >> m;
//枚举A、B
for (a = 0; a <= 1111; a++) {
for (b = 0; b <= 1111; b++) {
c = a + b; //用这种方式可以省掉枚举C优化代码复杂度
if (fun(a) + fun(b) + fun(c) == m-4) {
printf("%d + %d = %d\n", a, b, c);
sum++;
}
}
}
printf("一共有%d个等式\n", sum);
return 0;
}