全排列 next_permutation

来源:互联网 发布:eureka java demo 编辑:程序博客网 时间:2024/04/30 09:07

题目描述:

福尔摩斯到某古堡探险,看到门上写着一个奇怪的算式:
ABCDE * ? = EDCBA
他对华生说:“ABCDE应该代表不同的数字,问号也代表某个数字!”
华生:“我猜也是!”
于是,两人沉默了好久,还是没有算出合适的结果来。
请你利用计算机的优势,找到破解的答案。
把 ABCDE 所代表的数字写出来。

答案写在“解答.txt”中,不要写在这里! 

题目答案:

21978

题目思路:

ABCDE?这五个字符分别代码不同的数字(0-9中),将所有可能的情况枚举出来,找到符合的答案即可。可以用next_permutation()函数加快做题的速度。

题目代码:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include<cstdio>  
  2. #include<algorithm>  
  3. using namespace std;  
  4. int a[10] = {0,1,2,3,4,5,6,7,8,9};  
  5. int main(){  
  6.     do{  
  7.         //x1 x2 x3分别代表ABCDE 、? 和 EDCBA  
  8.         int x1 = a[0]*10000+a[1]*1000+a[2]*100+a[3]*10+a[4];  
  9.         int x2 = a[5];  
  10.         int x3 = a[0]+a[1]*10+a[2]*100+a[3]*1000+a[4]*10000;  
  11.         if(x1*x2==x3){  
  12.             printf("%d * %d = %d\n",x1,x2,x3);  
  13.         }  
  14.     }while(next_permutation(a,a+10));  
  15.     return 0;  
  16. }  





题目描述:

如图【1.png】所示六角形中,填入1~12的数字。

使得每条直线上的数字之和都相同。

图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少?

请通过浏览器提交答案,不要填写多余的内容。

题目答案:

10

题目思路:

用一个数组,存储每个位置上的数值,枚举全排列,判断符合条件的情况即可。

题目代码:

[cpp] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. #include<cstdio>  
  2. #include<algorithm>  
  3. using namespace std;  
  4. int a[12] = {1,2,3,4,5,6,7,8,9,10,11,12};  
  5. int main(){  
  6.     do{  
  7.         int x1 = a[0]+a[2]+a[5]+a[7];  
  8.         int x2 = a[0]+a[3]+a[6]+a[10];  
  9.         int x3 = a[7]+a[8]+a[9]+a[10];  
  10.         int x4 = a[1]+a[2]+a[3]+a[4];  
  11.         int x5 = a[1]+a[5]+a[8]+a[11];  
  12.         int x6 = a[4]+a[6]+a[9]+a[11];  
  13.         if(a[0]==1&&a[1]==8&&a[11]==3&&x1==x2&&x2==x3&&x3==x4&&x4==x5&&x5==x6){  
  14.             printf("%d\n",a[5]);  
  15.         }  
  16.           
  17.     }while(next_permutation(a,a+12));  
  18.       
  19.   
  20.     return 0;  
  21. }  
0 0