头像

Cyan

四川成都

深度强化学习炼丹师

2016年第七届蓝桥杯省赛-C. 凑算式

2016年第七届蓝桥杯省赛-C. 凑算式

2021-12-23 · 74次阅读 · 原创 · 数据结构与算法

原题链接

题面

     B      DEF
A + --- + ------- = 10
     C      GHI

这个算式中 A ~ I 代表 0 ~ 9 的数字,不同的字母代表不同的数字。

比如: 6+8/3+952/714 就是一种解法, 5+3/1+972/486 是另一种解法。

这个算式一共有多少种解法?

题解

枚举,全排列

利用 next_permutation 函数进行全排列枚举,按照下式进行带入计算:

a1+a2a3+a4a5a6a7a8a9=10a_1 + \frac{a_2}{a_3} + \frac{a_4a_5a_6}{a_7a_8a_9} = 10

答案:

29

代码

#include<iostream> #include<algorithm> using namespace std; int main() { int a[10]; int res = 0; int fz, fm; double s; for (int i = 0; i < 10; ++i) { a[i] = i; } do { fz = a[4] * 100 + a[5] * 10 + a[6]; fm = a[7] * 100 + a[8] * 10 + a[9]; int x = a[2] * fm + a[3] * fz, y = a[3] * fm; if (x % y != 0) continue; //不能被整除则跳过 s = a[1] + x / y; if (s == 10) { res++; } } while (next_permutation(a + 1, a + 10)); cout << res << endl; return 0; }

标题: 2016年第七届蓝桥杯省赛-C. 凑算式
链接: https://www.fightingok.cn/detail/183
更新: 2022-09-18 22:46:13
版权: 本文采用 CC BY-NC-SA 3.0 CN 协议进行许可