一、题目

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. 关于HBase Shell命令基本操作示例

    HBase 为用户提供了一个非常方便的使用方式, 我们称之为“HBase Shell”. HBase Shell 提供了大多数的 HBase 命令, 通过 HBase Shell 用户可以方便地创建. ...

  2. CQRS简单入门(Golang)

    一.简单入门之入门 CQRS/ES和领域驱动设计更搭,故整体分层沿用经典的DDD四层.其实要实现的功能概要很简单,如下图. 基础框架选择了https://github.com/looplab/even ...

  3. R语言学习笔记(十九):字符串处理中预定义字符组(表格介绍)

    R中预定义的字符组 代码 含义说明 [:digit:]或\\d 数字; [0-9] [^[:digit:]]或\\D 非数字; 等价于[^0-9] [:lower:] 小写字母; [a-z] [:up ...

  4. C# 用QQ企业邮箱发邮件

    问题System.Net.Mail下的SmtpClient来发送邮件,而System.Net.Mail only仅支持Explicit SSL 不要465端口,用25,不用EnableSsl = tr ...

  5. 20155213 《网络攻防》 Exp1 PC平台逆向破解

    20155213 <网络攻防> Exp1 PC平台逆向破解(5)M 实践内容 通过对实践对象--pwn20155213的linux可执行文件的修改或输入,完成以下三块: 手工修改可执行文件 ...

  6. 20145209刘一阳《JAVA程序设计》第五周课堂测试

    第五周课堂测试 1.下列关于内部类的说法,正确的是(ABD) A .其他类不可以用某个类的内部类声明对象. B .内部类字节码文件的名字格式是"外嵌类名$内部类名". C .内部类 ...

  7. JavaEE笔记(五)

    version 必须配置在id后面 缓存文件在映射文件后面 一级缓存:session回话级别 Session缓存的作用 (1)减少访问数据库的频率.应用程序从内存中读取持久化对象的速度显然比到数据库中 ...

  8. [并发并行]_[C/C++]_[C++标准库里的线程安全问题]

    场景 1.写普通的程序时, 经常会使用cout来做输出, 每个进程只有一个控制台, 如果多线程调用cout时会出状况吗? 2.之所以研究cout会不会在并发下调用有问题, 是因为曾经有一个bug的崩溃 ...

  9. 柯朗微积分与数学分析习题选解(1.1 节 a)

    一直在读<陶哲轩实分析>,陶的书非常的严谨,环环相扣,但是也有个缺点就是计算性的例子和应用方面的例子太少了.所以就又找了本柯朗的<微积分与数学分析>搭配着看.柯朗的书的习题与陶 ...

  10. P3164 [CQOI2014]和谐矩阵

    P3164 [CQOI2014]和谐矩阵 乱写能AC,暴力踩标程(雾 第一眼 诶这题能暴力枚举2333!!! 第二眼 诶这题能高斯消元!那只需要把每个位置的数给设出来就能够列方程了!然后就可以\(O( ...