zoukankan      html  css  js  c++  java
  • Backward Digit Sums(POJ 3187)

    Backward Digit Sums
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 5495   Accepted: 3184

    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.

    Source

    白书里面的例题,虽然要求搜索,但是next_permutation()都可以过。。
    题意:给定数字和,求原来的序列
    3   1   2   4
    
    4 3 6
    7 9
    16 给16,求3 1 2 4
     1 #include <cstdio>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <iostream>
     5 using namespace std;
     6 int n,a[15],sum,b[11][11];
     7 bool judge()
     8 {
     9     int i;
    10     for(i=0;i<n;i++)
    11         b[0][i]=a[i];
    12     int k=n-1,c=0;
    13     while(k--)
    14     {
    15         c++;
    16         for(i=0;i<k+1;i++)
    17             b[c][i]=b[c-1][i]+b[c-1][i+1];
    18     }
    19     //cout<<b[c][0]<<endl;;
    20     return b[c][0]==sum;
    21 }
    22 int main()
    23 {
    24     int i,j;
    25     while(scanf("%d%d",&n,&sum)!=EOF)
    26     {    
    27         for(i=0;i<n;i++)
    28             a[i]=i+1;
    29         do{
    30             if(judge())
    31                 break;
    32         }while(next_permutation(a,a+n));
    33         for(i=0;i<n;i++)
    34             printf("%d%c",a[i],i==(n-1)?'
    ':' ');
    35     }
    36 }
  • 相关阅读:
    zoj3888 找第二大
    zoj3882 博弈
    字典树小总结
    hdu2222 字典树
    hdu1247 字典树
    开放融合 | “引擎级”深度对接!POLARDB与SuperMap联合构建首个云原生时空平台
    阿里HBase高可用8年“抗战”回忆录
    最佳实践 | RDS & POLARDB归档到X-Pack Spark计算
    今日头条在消息服务平台和容灾体系建设方面的实践与思考
    饿了么监控系统 EMonitor 与美团点评 CAT 的对比
  • 原文地址:https://www.cnblogs.com/a1225234/p/5150849.html
Copyright © 2011-2022 走看看