C - Backward Digit Sums
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice 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.
题意:
给出N与总数M,N指从1到N一共有N个数,通过之间任意排序,相邻两个数两两加和最后得出一个总数,当这个总数等于M时,则输出该序列,这个序列可能不止一种,输出字典序最小的一组数据。
思路:
先用二维数组将这N个数放在第一行位置上,然后通过调用algorithm函数中的next_permutation来对刚行数据不停生成新排序序列来进行两两加和处理,当一遇到和等于M时,输出该序列。
AC and test:
#include<stdio.h> #include<algorithm> #include<string.h> #include<stdlib.h> using namespace std; int main() { int N,M,i,j; int a[15][15]; memset(a,0,sizeof(a)); scanf("%d%d",&N,&M); for(i=0;i<N;i++) a[0][i]=i+1; // for(i=0;i<N;i++) // printf("%d ",a[0][i]); // printf("\n"); // sort(a[0],a[0]+N); // for(i=0;i<N;i++) // printf("%d ",a[0][i]); // printf("\n"); do { for(i=1;i<N;i++) for(j=0;j<N-i;j++) a[i][j]=a[i-1][j]+a[i-1][j+1]; // for(i=0;i<N;i++) // { // for(j=0;j<N;j++) // printf("%d ",a[i][j]); // printf("\n"); // } //printf("%d\n",a[N-1][0]); // system("pause"); if(a[N-1][0]==M) { for(i=0;i<N;i++) { printf("%d",a[0][i]); i==N?printf("\n"):printf(" "); } break; } }while(next_permutation(a[0],a[0]+N)); //不断的生成新序列 }
总结:
好好看书非常重要……先把书本的学会了再说……
相关推荐
提出一种利用UIO生成 BUIO(Backward UIO)的方法,部分UIO直接转换成BUIO,可以降低BUIO的生成费用。对基于UIO和BUIO的启发式测试序列生成方法进行了分析,完善了该方法的生成算法,保证测试序列的自动生成。以ECMA...
BACKWARD
赠送jar包:lucene-backward-codecs-6.6.0.jar; 赠送原API文档:lucene-backward-codecs-6.6.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
Backward Sky多媒体教室预定系统 Ver20081126 比上个版本有如下更新: 1、修正了源代码没有EXCEL文件夹的BUG 2、图形界面显示模式改为当前一周及下一周 3、图形界面显示多媒体教室超过3个自动换行 4、预定图形界面...
赠送jar包:lucene-backward-codecs-7.2.1.jar; 赠送原API文档:lucene-backward-codecs-7.2.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
赠送jar包:lucene-backward-codecs-7.3.1.jar; 赠送原API文档:lucene-backward-codecs-7.3.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
The forward-backward algorithm.pdf
forward sweep and backward sweep load flow
A simple introductory of Euler Numerical Method, which is fairly important in our daily numerical calculations.
backward forward sweep method for radial distribution system
此代码对特定点的给定数据集进行插值、区分(最高为任意顺序)和评估。 由于此代码使用牛顿的前进/后退和 Sterling 方法,因此自变量的区间必须... 首先,它使用数据集生成一个函数,然后根据需要对其进行评估或区分。
赠送jar包:lucene-backward-codecs-7.7.0.jar; 赠送原API文档:lucene-backward-codecs-7.7.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
赠送jar包:lucene-backward-codecs-7.2.1.jar; 赠送原API文档:lucene-backward-codecs-7.2.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
赠送jar包:lucene-backward-codecs-7.3.1.jar; 赠送原API文档:lucene-backward-codecs-7.3.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
赠送jar包:lucene-backward-codecs-6.6.0.jar; 赠送原API文档:lucene-backward-codecs-6.6.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
Step by step forward and backward sweep
1、参考文献:Forward-Backward Error: Automatic Detection of Tracking Failures,文件附带参考文献 2、用matlab、C、Opencv 混合编程实现 3、遇到不兼容时,请在mex文件夹从新编译生成新的“mex”文件
赠送jar包:lucene-backward-codecs-7.7.0.jar; 赠送原API文档:lucene-backward-codecs-7.7.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
译文 TLD:Forward-Backward Error Automatic Detection of Tracking Failures 翻译 水平有限,仅供误导。(交流贴,欢迎站内信指正。)
Backward Doubly Stochastic Differential Equations with Jumps and Stochastic Partial Differential-Integral Equations,朱庆峰,石玉峰,A type of backward doubly stochastic differential equations driven ...