【搜索】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& ...
随机推荐
- CTF-i春秋网鼎杯第二场misc部分writeup
CTF-i春秋网鼎杯第二场misc部分writeup 套娃 下载下来是六张图片 直接看并没有什么信息 一个一个查看属性 没有找到有用信息 到winhexv里看一下 都是标准的png图片,而且没有fla ...
- 20155213 《网络攻防》 Exp1 PC平台逆向破解
20155213 <网络攻防> Exp1 PC平台逆向破解(5)M 实践内容 通过对实践对象--pwn20155213的linux可执行文件的修改或输入,完成以下三块: 手工修改可执行文件 ...
- JavaWeb总结(七)
Web状态管理 - HTTP协议使用的是无状态的连接 - 对容器而言,每一个请求都来自于一个新的客户 解决方案-表单隐藏字段 <input type=”hidden” name=”session ...
- 查询红帽linux/Oracle Linux的发行版本的方法
[root@localhost ~]# lsb_release -aLSB Version: :core-4.0-amd64:core-4.0-ia32:core-4.0-noarch:grap ...
- Oracle中实现dblink的作法
基本环境: 机器1: 192.168.56.102 用作dblink的使用者(create database link 语句在此执行) 机器2: 192.168.56.103 用作dblink的源 ...
- re 模块错误 error: bad character range
下午,看到堆栈的内容.于是上机实验了一番 >>> bds = '10+6/5-4*2' # 数学运算表达式 想用 findall 把运算符号提取出来 >>> imp ...
- 【HEOI2016】序列
题面 题解 很像最长不下降子序列对吧(废话) 设$up[i]$和$down[i]$分别表示$i$最大最小能取多少 注意到: $$ f[i] = max_j\left\{f[j]\right\} + 1 ...
- Git的原理简介和常用命令
Git和SVN是我们最常用的版本控制系(Version Control System, VCS),当然,除了这二者之外还有许多其他的VCS,例如早期的CVS等.顾名思义,版本控制系统主要就是控制.协调 ...
- 用Angule Cli创建Angular项目
Angular4.0来了,更小,更快,改动少 接下来为Angular4.0准备环境和学会使用Angular cli项目 1.环境准备: 1)在开始工作之前我们必须设置好开发环境 如果你的机器上还没有安 ...
- C# string 的一点属性、方法什么的
今天学的基本可以说是都属于方法和属性 下面这两句话非常重要,确实非常重要 凡是可以 “ . ” 出来的,前面是黑色的小扳手的:属性 紫色的立方体的:方法 这个对于以后自学帮助是不小的,当然, ...