【搜索】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 n…
POJ 3187  Backward Digit Sums http://poj.org/problem?id=3187 题目大意: 给你一个原始的数字序列: 3   1   2   4  他可以相邻的元素相加得到 4 3 6 然后 7 9 最后得到16,现在给定序列的长度,还有最后的得数,求原始序列(多解则取最小) 思路: 直接枚举即可. 下面是next_permutation版本. #include<cstdio> #include<cstdlib> #include<c…
POJ 3187 给定N值,从而确定了数据的范围及长度,暴力枚举数列,接下来类似杨辉三角的递推计算.注permutation从递增有序数列开始枚举,枚举到符合sum值时退出即可 #include <stdio.h> #include <algorithm> using namespace std; int n; int sum; int arr[10][10];//中间结果 int main() { while (scanf("%d %d", &n,&a…
题意:已知有N个数分别为1-N,如下图为4个数.相邻两两相加直至只剩下一个数,下图的结果就是16. 3 1 2 4     4 3 6   7 9 16 现在反过来看,告诉你数的个数N和最终结果,问这N个数的初始序列是什么.求出字典序最小的初始序列.上图的初始序列也可以是3 2 1 4,但这不是字典序最小. 分析:这题用全排列的方式非常容易做.首先初始化数组为1-N,然后用STL提供的按字典序生成全排列的函数next_permutation即可枚举全排列.对于每一组数,通过计算可以知道它是否能得…
题目:http://poj.org/problem?id=3187 题意: 像这样,输入N : 表示层数,输入over表示最后一层的数字,然后这是一个杨辉三角,根据这个公式,由最后一层的数,推出第一行的数字(由1~N组成).如果有多个解,按字典序升序,输出第一个解. 题解:水题,不多说 AC代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> usi…
题目链接:http://poj.org/problem?id=3187 解题报告: #include <stdio.h> #include <iostream> #include <algorithm> using namespace std; int main() { int n,sum; scanf("%d%d",&n,&sum); ],b[]; ; i<n; i++) a[i]=i+; do { ;i<n;i++)…
如果给出一个由1~n组成的序列,我们可以每相邻2个数求和,得到一个新的序列,不断重复,最后得到一个数sum, 现在输入n,sum,要求输出一个这样的排列,如果有多种情况,输出字典序最小的那一个. 刚开始我是直接搜,tle了 然后就开始找最初的序列和最终的和有什么关系 因为最终的和sum一定是等于若干个a[1],若干个a[2],...,若干个a[n]的和 即sum=p1*a1+p2*a2+...+pn*an 所以我们只要求出数组a的系数,n个p即可. 然后发现,和杨辉三角有很大的关系. 如果杨辉三…
-->Backward Digit Sums 直接写中文了 Descriptions: FJ 和 他的奶牛们在玩一个心理游戏.他们以某种方式写下1至N的数字(1<=N<=10). 然后把相邻的数相加的到新的一行数.重复这一操作直至只剩一个数字.比如下面是N=4时的一种例子 3 1 2 4 4 3 6 7 9 16 在FJ回来之前,奶牛们开始了一个更难的游戏:他们尝试根据最后结果找到开始的序列.这已超过了FJ的思考极限. 写一个程序来帮助FJ吧 Input N和最后的和 Output 满足…
Description FJ and his cows enjoy playing a mental game. They write down the numbers to N ( <= N <= ) ) might go like Behind FJ's back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from on…
思路: next_permutation()加个递推组合数随便搞搞就A了- //By SiriusRen #include <cstdio> #include <algorithm> using namespace std; int n,C[11][11],sum,f[11]; int main(){ scanf("%d%d",&n,&sum); for(int i=1;i<=n;i++)C[i][i]=1,f[i]=i; for(int…