一、题目

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. MAC上安装GCC失败

    问题 在用brew安装GCC时, 报了如下错误. ➜ ~ brew install gcc ==> Installing dependencies for gcc: isl, mpfr and ...

  2. day 87 Vue学习六之axios、vuex、脚手架中组件传值

      本节目录 一 axios的使用 二 vuex的使用 三 组件传值 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 axios的使用 Axios 是一个基于 promise 的 HT ...

  3. 01.创建winserver2012r2+hyper-v+centos7

    1.背景 DELL poweredge T320,装的winserver2012 r2,利用自带的hyper-v安装centos7,后期主要用于spark开发. 1.1 安装winserver2012 ...

  4. PyQt5在QWidget窗体中显示Qwidget的自定义类(补:完美解决)

    [概览] 1.显示原生Qwidget 1)不使用布局(绝对定位) 2)使用布局 2.显示Qwidget的自定义类 1)不使用布局(绝对定位)       2)使用布局 [知识点] 1.显示原生Qwid ...

  5. 4827: [Hnoi2017]礼物

    4827: [Hnoi2017]礼物 链接 分析: 求最小的$\sum_{i=1}^{n}(x_i-y_i)^2$ 设旋转了j位,每一位加上了c. $\sum\limits_{i=1}^{n}(x_{ ...

  6. JAVAWEB tomcat服务器启动错误原因总结

    tomcat服务器启动错误: org.apache.catalina.LifecycleException    这种异常的原因是  servlet的代码出现了错误 实例: 这里的servlet由于使 ...

  7. SpringMVC 异常信息ASM ClassReader failed to parse class file的问题解决

    1.  环境信息: Spring 3.2.0,  JDK 1.8.0 2.  运行简单的程序,出现以下错误信息: 2.  运行简单的程序,出现以下错误信息: Caused by: org.spring ...

  8. centos7 安装postgres9.4

    1.安装postgres资源:> yum install https://download.postgresql.org/pub/repos/yum/9.4/redhat/rhel-7-x86_ ...

  9. 设置JFrame背景图片

    这里我就放上改写的代码吧,不做多的解释,推荐一个好的博文 https://blog.csdn.net/jdsjlzx/article/details/16831815 public void ini_ ...

  10. python类与对象的组合与继承

    1.把类的对象实例化放到一个新的类里面叫做类的组合,组合就是指几个横向关系的类放在一起,纵向关系的类放在一起是继承,根据实际应用场景确定.简单的说,组合用于“有一个”的场景中,继承用于“是一个”的场景 ...