2014第五届蓝桥杯C++B组第七题
2016-03-02 00:23
253 查看
第7题:六角填数(12')
如图所示六角形中,填入1~12的数字。
使得每条直线上的数字之和都相同。
图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少?
请通过浏览器提交答案,不要填写多余的内容。
思路就是暴力解决 蓝桥杯还没有时间限制
之间运用了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
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
//int a[4][10];
int sum[10];
int a[15]={0,2,4,5,6,7,9,10,11,12};
int main()
{
for(int i=1;i>0;i++)
{
next_permutation(a+1,a+10);
sum[1]=1+a[1]+a[4]+a[6];
sum[2]=1+a[2]+a[5]+a[9];
sum[3]=a[6]+a[7]+a[8]+a[9];
sum[4]=8+a[2]+a[1]+a[3];
sum[5]=8+a[4]+a[7]+3;
sum[6]=3+a[8]+a[5]+a[3];
if(sum[1]==sum[2]&&sum[1]==sum[3]&&sum[1]==sum[4]&&sum[1]==sum[5]&&sum[1]==sum[6])
break;
}
for(int i=1;i<10;i++)
printf("%d-%d\n",i,a[i]);
return 0;
}
如图所示六角形中,填入1~12的数字。
使得每条直线上的数字之和都相同。
图中,已经替你填好了3个数字,请你计算星号位置所代表的数字是多少?
请通过浏览器提交答案,不要填写多余的内容。
思路就是暴力解决 蓝桥杯还没有时间限制
之间运用了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
#include <iostream>
#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
//int a[4][10];
int sum[10];
int a[15]={0,2,4,5,6,7,9,10,11,12};
int main()
{
for(int i=1;i>0;i++)
{
next_permutation(a+1,a+10);
sum[1]=1+a[1]+a[4]+a[6];
sum[2]=1+a[2]+a[5]+a[9];
sum[3]=a[6]+a[7]+a[8]+a[9];
sum[4]=8+a[2]+a[1]+a[3];
sum[5]=8+a[4]+a[7]+3;
sum[6]=3+a[8]+a[5]+a[3];
if(sum[1]==sum[2]&&sum[1]==sum[3]&&sum[1]==sum[4]&&sum[1]==sum[5]&&sum[1]==sum[6])
break;
}
for(int i=1;i<10;i++)
printf("%d-%d\n",i,a[i]);
return 0;
}
相关文章推荐
- c语言:用函数找大数
- c语言面试题1
- 《Effective C++》学习笔记——条款39
- libc++abi.dylib: terminate_handler unexpectedly
- 【小白】如何在GNU Emacs环境下进行C++编程及编译
- Effective C++: RVO, NRVO.
- c++程序员不可不知的101条经验-检测和定位内存泄漏的技巧
- 跨平台C++服务器程序开发 (2)服务器端口
- 结构体嵌套对齐
- [C语言][LeetCode][206]Reverse Linked List
- C++ namespace功能总结
- 任一个英文的纯文本文件0004.txt,统计其中的单词(不区分大小写)出现的个数
- c++笔试题
- C++ 字符串防止溢出:strncpy,snprintf,strnlen,memset的使用
- C++_将图二维矩阵形式转为邻接表结构
- 如何限制类仅能创建一个对象
- C++基础总结(2)---字符串
- C++基础总结(2)---字符串
- [C语言][LeetCode][283]Move Zeroes
- 杨鑫的C++上机报告