一、题目

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 枚举全排列的更多相关文章

  1. POJ 3187 Backward Digit Sums 枚举水~

    POJ 3187  Backward Digit Sums http://poj.org/problem?id=3187 题目大意: 给你一个原始的数字序列: 3   1   2   4  他可以相邻 ...

  2. POJ 3187【permutation】

    POJ 3187 给定N值,从而确定了数据的范围及长度,暴力枚举数列,接下来类似杨辉三角的递推计算.注permutation从递增有序数列开始枚举,枚举到符合sum值时退出即可 #include &l ...

  3. POJ 3187 穷举

    题意:已知有N个数分别为1-N,如下图为4个数.相邻两两相加直至只剩下一个数,下图的结果就是16. 3 1 2 4     4 3 6   7 9 16 现在反过来看,告诉你数的个数N和最终结果,问这 ...

  4. 穷竭搜索:POJ 3187 Backward Digit Sums

    题目:http://poj.org/problem?id=3187 题意: 像这样,输入N : 表示层数,输入over表示最后一层的数字,然后这是一个杨辉三角,根据这个公式,由最后一层的数,推出第一行 ...

  5. next_permutation暴力搜索,POJ(3187)

    题目链接:http://poj.org/problem?id=3187 解题报告: #include <stdio.h> #include <iostream> #includ ...

  6. POJ 3187 杨辉三角+枚举排列 好题

    如果给出一个由1~n组成的序列,我们可以每相邻2个数求和,得到一个新的序列,不断重复,最后得到一个数sum, 现在输入n,sum,要求输出一个这样的排列,如果有多种情况,输出字典序最小的那一个. 刚开 ...

  7. 【POJ - 3187】Backward Digit Sums(搜索)

    -->Backward Digit Sums 直接写中文了 Descriptions: FJ 和 他的奶牛们在玩一个心理游戏.他们以某种方式写下1至N的数字(1<=N<=10). 然 ...

  8. poj 3187 Backward Digit Sums(穷竭搜索dfs)

    Description FJ and his cows enjoy playing a mental game. They write down the numbers to N ( <= N ...

  9. POJ 3187 全排列+杨辉三角(组合数)

    思路: next_permutation()加个递推组合数随便搞搞就A了- //By SiriusRen #include <cstdio> #include <algorithm& ...

随机推荐

  1. jQuery----左侧导航栏面板切换实现

    页面运行结果:                                                      点击曹操 点击刘备 点击孙权 原图 需求说明:原图如上所示,点击一方诸侯的时候 ...

  2. C++快速开发样本工程的建立--简介

    背景 在开发项目过程中,一些功能库能反复被写被用,可以写成库被重用: 但是行业业务也随着换项目,每次重新写一次,如果把一些功能业务和框架绑定,配置绑定,只需要添加,修改,增加业务功能,就可以搭建C++ ...

  3. JSON解析工具——Jackson的简单使用

    什么是Jackson 可以轻松实现Java对象与JSON字符串的转换 准备工作:导包 Jackson的jar all下载地址:http://jackson.codehaus.org/1.7.6/jac ...

  4. My97DatePicker:开始时间和结束时间的最大间隔为1个月30天,并且不大于当前时间(3种方法)

    问题的背景 在之前做Web项目的时候,开始时间和结束时间,只有2个要求: 1.开始时间必须小于等于结束时间,不能超过当前时间. 2.结束时间必须大于等于开始时间,不能超过当前时间. 由于开始时间不大于 ...

  5. Velocity学习3

    Velocity快速入门教程 Apache的速度是一个基于Java的的模板引擎(模板引擎),它允许任何人仅仅简单的使用模板语言(模板语言)来引用由java的代码定义的对象. 官网介绍如下: Veloc ...

  6. 04 - django的路由层 1

    1.路由控制简单配置 from django.conf.urls import url from . import views urlpatterns = [ url(r'^articles/2003 ...

  7. 5285: [Hnoi2018]寻宝游戏

    5285: [Hnoi2018]寻宝游戏 链接 分析: 从下面依次确定运算符号,然后在确定的过程中,需要确定的位数会逐渐减少.比如最后有一个1,如果在从下往上确定了一个or 1,那么再往前可以随便选了 ...

  8. BZOJ3196 二逼平衡树 ZKW线段树套vector(滑稽)

    我实在是不想再打一遍树状数组套替罪羊树了... 然后在普通平衡树瞎逛的时候找到了以前看过vector题解 于是我想:为啥不把平衡树换成vector呢??? 然后我又去学了一下ZKW线段树 就用ZKW线 ...

  9. JZOJ 10043 第k小数

    Description 有两个非负整数数列,元素个数分别为N和M.从两个数列中分别任取一个数相乘,这样一共可以得到NM个数,询问这NM个数中第K小数是多少. 时间限制为20ms . Input 输入文 ...

  10. this四种绑定方式之间的奇淫技巧

    写在前面 上一篇中,我们对于JavaScript中原始值.复杂值以及内存空间进行了一个深入浅出的总结,这次我们来聊一聊JavaScript中this关键字的深入浅出的用法. 在 JavaScript ...