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

    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.

     

    无脑枚举全排列,竟然都过了,状态总数只有10!

    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <string>
    #include <map>
    #include <vector>
    #include <list>
    #include <set>
    #include <stack>
    #include <queue>
    #include <deque>
    #include <algorithm>
    #include <functional>
    #include <iomanip>
    #include <limits>
    #include <new>
    #include <utility>
    #include <iterator>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    #include <cmath>
    #include <ctime>
    using namespace std;
    const int INF = 0x3f3f3f3f;
    const double PI = acos(-1.0);
    int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};
    const int maxn = 10000010;
    
    int main()
    {
        int n, sum;
        cin >> n >> sum;
        int s[15];
        for (int i = 0; i < n; ++i)
            s[i] = i + 1;
        do
        {
            int t[15];
            for (int i = 0; i < n; ++i)
                t[i] = s[i];
            int tmp = n;
            while (tmp-- > 1)
                for (int i = 0; i < tmp; ++i)
                    t[i] += t[i+1];
            if (t[0] == sum)
            {
                for (int i = 0; i < n-1; ++i)
                    printf("%d ", s[i]);
                printf("%d
    ", s[n-1]);
                break;
            }
        } while (next_permutation(s, s+n));
        return 0;
    }


     

  • 相关阅读:
    如何使网站不出现从客户端(:)中检测到有潜在危险的 Request.Path 值这样类似的提示
    layui下select下拉框不显示或没有效果
    video
    各个平台的mysql重启命令
    微信开放平台 redirect_uri参数错误
    JS中setTimeout()的用法详解
    验证视图状态MAC失败的解决办法
    The 'microsoft.jet.oledb.4.0' provider is not registered on the local machin
    一个Nginx部署多个vue前端项目总结
    Java之HttpClient调用WebService接口发送短信源码实战
  • 原文地址:https://www.cnblogs.com/godweiyang/p/12203987.html
Copyright © 2011-2022 走看看