2014第五届蓝桥杯C++B组第七题

第7题:六角填数(12')


    如图所示六角形中,填入1~12的数字。
    使得每条直线上的数字之和都相同。
    图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少?

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

2014第五届蓝桥杯C++B组第七题_第1张图片


思路就是暴力解决  蓝桥杯还没有时间限制 

之间运用了STL的一个函数 next_permutation();
     next_permutation();是一个用来来寻找一组序列的下一个排列的函数   使用时与sort类似

解释一下什么是一个序列的下一个排列:
     比如有一个序列 1 2 3  他的下一个排列就是1 3 2
这是所有1 2 3的所有排列
     1 3 2
     1 3 2
     2 1 3
     2 3 1
     3 1 2
     3 2 1
当本次排列已经是最后一个排列的时候 他的下一个排列就是第一个排列

附道题吧 http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=2005
这道题仅用这个函数就能做出来 题目对本函数的描述也是很详细的

[cpp]  view plain  copy
 
  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5. #include   
  6. using namespace std;  
  7. //int a[4][10];  
  8. int sum[10];  
  9. int a[15]={0,2,4,5,6,7,9,10,11,12};  
  10.   
  11. int main()  
  12. {  
  13.   
  14.     for(int i=1;i>0;i++)  
  15.     {  
  16.         next_permutation(a+1,a+10);    
  17.         sum[1]=1+a[1]+a[4]+a[6];  
  18.         sum[2]=1+a[2]+a[5]+a[9];  
  19.         sum[3]=a[6]+a[7]+a[8]+a[9];  
  20.         sum[4]=8+a[2]+a[1]+a[3];  
  21.         sum[5]=8+a[4]+a[7]+3;  
  22.         sum[6]=3+a[8]+a[5]+a[3];  
  23.         if(sum[1]==sum[2]&&sum[1]==sum[3]&&sum[1]==sum[4]&&sum[1]==sum[5]&&sum[1]==sum[6])  
  24.             break;  
  25.   
  26.     }  
  27.     for(int i=1;i<10;i++)  
  28.     printf("%d-%d\n",i,a[i]);  
  29.     return 0;  
  30. }  

你可能感兴趣的:(STL)