蓝桥杯2016届省赛B组(凑算式)

142 阅读1分钟

题目描述

          B          DEF
A +    —    +  ——— = 10
C          GHI
这个算式中AI代表19的数字,不同的字母代表不同的数字。
比如:
6+8/3+952/714 就是一种解法,
5+3/1+972/486 是另一种解法。

这个算式一共有多少种解法?
注意:你提交应该是个整数,不要填写任何多余的内容或说明性文字。

由于在<algorithm>库中有一个很好的函数(next_permutation)

于是代码如下:

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
	int a[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
	int n = 0;
	do {
		float x = a[0];
		float y = (1.0 * a[1]) / (1.0 * a[2]);
		float z = (100.0 * a[3] + 10.0 * a[4] + 1.0 * a[5]) / (100.0 * a[6] + 10.0 * a[7] + 1.0 * a[8]);
		if (x + y + z == 10.0) {
			n++;
		}
	} while (next_permutation(a, a + 9));
	cout << n;
	return 0;
}

answer: