蓝桥杯 马虎的算式(枚举全排列)


标题: 马虎的算式


小明是个急性子,上小学的时候经常把老师写在黑板上的题目抄错了。

有一次,老师出的题目是:36 x 495 = ?

他却给抄成了:396 x 45 = ?

但结果却很戏剧性,他的答案竟然是对的!!

因为 36 * 495 = 396 * 45 = 17820

类似这样的巧合情况可能还有很多,比如:27 * 594 = 297 * 54

假设 a b c d e 代表1~9不同的5个数字(注意是各不相同的数字,且不含0)

能满足形如: ab * cde = adb * ce 这样的算式一共有多少种呢?


请你利用计算机的优势寻找所有的可能,并回答不同算式的种类数。

满足乘法交换律的算式计为不同的种类,所以答案肯定是个偶数。


答案直接通过浏览器提交。
注意:只提交一个表示最终统计种类数的数字,不要提交解答过程或其它多余的内容。

思路:枚举1-9九个数的全排列,取前5个分别为a,b,c,d,e,代入判断即可。但是要注意判重!!!  因为枚举的是9个数的全排列,但是只用到了前5个数作为abcde,同样的abcde会重复出现。

 1 #include<iostream>
 2 #include<string>
 3 #include<queue>
 4 #include<set>
 5 #include<cstring>
 6 #include<cmath>
 7 #include<algorithm>
 8 #include<stdio.h>
 9 
10 using namespace std;
11 
12 int vis[10][10][10][10][10];
13 
14 int main()
15 {
16     int a[9] = {1,2,3,4,5,6,7,8,9};
17     int cnt = 0;
18     memset(vis, 0, sizeof(vis));
19     
20     do
21     {
22         //注意判重! 
23         if((!vis[a[0]][a[1]][a[2]][a[3]][a[4]]) && ((a[0]*10+a[1]) * (a[2]*100+a[3]*10+a[4])) == ((a[0]*100+a[3]*10+a[1])*(a[2]*10+a[4])))
24         {
25             vis[a[0]][a[1]][a[2]][a[3]][a[4]] = 1;
26             printf("%d%d * %d%d%d = %d%d%d * %d%d\n", a[0], a[1], a[2], a[3], a[4], a[0],a[3],a[1],a[2],a[4]);
27             cnt++;
28         }
29         
30         
31     }while(next_permutation(a,a+9));
32 
33     cout << cnt << endl;
34     return 0;
35 }

猜你喜欢

转载自www.cnblogs.com/FengZeng666/p/10548198.html