【第七届蓝桥杯】凑算式

来源:互联网 发布:sql两表连接查询 编辑:程序博客网 时间:2024/05/17 07:43
题目:凑算式

       B      DEF
A + --- + ------- = 10
       C      GHI
     
(如果显示有问题,可以参见【图1.jpg】)

 

图1.jpg

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

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

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

注意:你提交应该是个整数,不要填写任何多余的内容或说明性文字。


答案:29


思路:避免整数除法

假设 X = DEF, Y = GHI, 等式两边都乘于CY 
A*C*Y + B*Y + C*X = 10*C*Y  
B*Y + C*X = (10-A)*C*Y 


C++代码一:

#include <iostream>#include <algorithm>using namespace std;int main() {int sum = 0;int v[10] = {1,2,3,4,5,6,7,8,9};do {if(v[0]+v[1]/(double)v[2]+(v[3]*100+v[4]*10+v[5])/(double)(v[6]*100+v[7]*10+v[8]) == 10)sum++;} while(next_permutation(v, v+9));cout << sum;return 0;}

C++代码二:

#include <algorithm>         // next_permutation #include <iostream>using namespace std;int main(){//             A  B  C  D  E  F  G  H  I   依次对应 a[0] 到 a[8]  int a[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };    // 从小到大排好序  int x, y, count = 0;do {x = a[3] * 100 + a[4] * 10 + a[5];y = a[6] * 100 + a[7] * 10 + a[8];if (a[1] * y + a[2] * x == (10 - a[0]) * a[2] * y)++count;} while ( next_permutation(a, a+9) );cout << count << endl;return 0;}

C++代码三:

#include <algorithm>         // prev_permutation  #include <iostream>using namespace std;int main(){//             A  B  C  D  E  F  G  H  I   依次对应 a[0] 到 a[8] int a[9] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };    // 从大到小排好序  int x, y, count = 0;do {x = a[3] * 100 + a[4] * 10 + a[5];y = a[6] * 100 + a[7] * 10 + a[8];if (a[1] * y + a[2] * x == (10 - a[0]) * a[2] * y)++count;} while ( prev_permutation(a, a+9) );cout << count << endl;return 0;}

第七届蓝桥杯所有组试题与部分答案

阅读全文
0 0
原创粉丝点击