【搜索】POJ-3187 枚举全排列
一、题目
Description
FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. For example, one instance of the game (when N=4) might go like this:
3 1 2 4
4 3 6
7 9
16
Behind FJ's back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N. Unfortunately, the game is a bit above FJ's mental arithmetic capabilities.
Write a program to help FJ play the game and keep up with the cows.
Input
Line 1: Two space-separated integers: N and the final sum.
Output
Line 1: An ordering of the integers 1..N that leads to the given sum. If there are multiple solutions, choose the one that is lexicographically least, i.e., that puts smaller numbers first.
Sample Input
4 16
Sample Output
3 1 2 4
Hint
Explanation of the sample:
There are other possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.
二、思路&心得
- 利用next_permutation()函数生成全排列,暴力搜索
三、代码
#include<cstdio>
#include<algorithm>
using namespace std;
int N, sum;
int a[11], b[11];
void solve() {
for (int i = 0; i < N; i ++) {
a[i] = i + 1;
}
do {
for (int i = 0; i < N; i ++) {
b[i] = a[i];
}
for (int i = N - 1; i > 0; i --) {
for (int j = 0; j < i; j ++) {
b[j] += b[j + 1];
}
}
if (b[0] == sum) {
for (int i = 0; i < N; i ++) {
printf("%d ", a[i]);
}
printf("\n");
break;
}
} while (next_permutation(a, a + N));
}
int main() {
while (~scanf("%d %d", &N, &sum)) {
solve();
}
return 0;
}
【搜索】POJ-3187 枚举全排列的更多相关文章
- POJ 3187 Backward Digit Sums 枚举水~
POJ 3187 Backward Digit Sums http://poj.org/problem?id=3187 题目大意: 给你一个原始的数字序列: 3 1 2 4 他可以相邻 ...
- POJ 3187【permutation】
POJ 3187 给定N值,从而确定了数据的范围及长度,暴力枚举数列,接下来类似杨辉三角的递推计算.注permutation从递增有序数列开始枚举,枚举到符合sum值时退出即可 #include &l ...
- POJ 3187 穷举
题意:已知有N个数分别为1-N,如下图为4个数.相邻两两相加直至只剩下一个数,下图的结果就是16. 3 1 2 4 4 3 6 7 9 16 现在反过来看,告诉你数的个数N和最终结果,问这 ...
- 穷竭搜索:POJ 3187 Backward Digit Sums
题目:http://poj.org/problem?id=3187 题意: 像这样,输入N : 表示层数,输入over表示最后一层的数字,然后这是一个杨辉三角,根据这个公式,由最后一层的数,推出第一行 ...
- next_permutation暴力搜索,POJ(3187)
题目链接:http://poj.org/problem?id=3187 解题报告: #include <stdio.h> #include <iostream> #includ ...
- POJ 3187 杨辉三角+枚举排列 好题
如果给出一个由1~n组成的序列,我们可以每相邻2个数求和,得到一个新的序列,不断重复,最后得到一个数sum, 现在输入n,sum,要求输出一个这样的排列,如果有多种情况,输出字典序最小的那一个. 刚开 ...
- 【POJ - 3187】Backward Digit Sums(搜索)
-->Backward Digit Sums 直接写中文了 Descriptions: FJ 和 他的奶牛们在玩一个心理游戏.他们以某种方式写下1至N的数字(1<=N<=10). 然 ...
- poj 3187 Backward Digit Sums(穷竭搜索dfs)
Description FJ and his cows enjoy playing a mental game. They write down the numbers to N ( <= N ...
- POJ 3187 全排列+杨辉三角(组合数)
思路: next_permutation()加个递推组合数随便搞搞就A了- //By SiriusRen #include <cstdio> #include <algorithm& ...
随机推荐
- zabbix服务快速搭建指南
zabbix监控服务快速安装指南 1.更换centos7-base源为阿里源 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.ali ...
- 关于 class helper for ... 语法
转载于万一的博客https://www.cnblogs.com/del/archive/2009/10/13/1582789.html class helper 可能是从 Delphi 2007 增加 ...
- python爬虫-execjs使用
python爬虫-execjs使用 ecexjs的作用 通过python代码去执行JavaScript代码的库 execjs的安装 pip install PyExecJS execjs使用之前,得先 ...
- c语言数组放在main函数里面和外面的区别
最近a算法题的时候碰到一道题:一个数列前三项都为1,之后每项的值等于前三项之和,求第20193024项的最后4位数字.一开始写的代码如下: 结果一直爆 Terminated due to signal ...
- win10操作系统vs2010编译osg3.4.0问题解决记录
参考博客:OSG3.4.0+VS2010+WIN10编译及二次开发环境搭建 链接:https://blog.csdn.net/hsc1239653453/article/details/7827856 ...
- 2_C语言中的数据类型 (四)整数与无符号数
1.1 sizeof关键字 sizeof是c语言关键字,功能是求指定数据类型在内存中的大小,单位:字节 sizeof与size_t类型 1.1 int类型 1.1.1 ...
- HDU - 3874 Necklace (树状数组、离线处理)
题目链接:Necklace 题意: 给出一串珠子,每个珠子有它的value,现在给出n(n<=5e4)个珠子的value(1<=value<=1e6),现在给出m(1<=m&l ...
- linux bash Shell脚本经典 Fork炸弹演示及命令详解
Jaromil 在 2002 年设计了最为精简的一个Linux Fork炸弹,整个代码只有13个字符,在 shell 中运行后几秒后系统就会宕机: :(){:|:&};: 这样看起来不是很好理 ...
- mysql批量新增或者更新
1.批量更新或者新增 1.单个新增或者更新 keyProperty新增完之后返回Id值
- Received non-all-whitespace CHARACTERS or CDATA event in nextTag(). ,无法整齐打印验证错误。 解析XML文档出现的问题
在启动keyCloak,想要在standAlone模式下切换数据库,修改standAlone.xml文件时. 在bin/目录下启动standAlone模式出现错误: 10:07:24,799 INFO ...