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;
    }


     

  • 相关阅读:
    c# 文件上传
    iOSswift基础篇1
    copyWithZone 的使用方法
    客户端登陆接收大量数据导致数据丢失问题解决方法
    设计模式观察者模式(KVO)
    SQLite 之 C#版 System.Data.SQLite 使用
    设计模式MVC(C++版)
    JS 创建自定义对象的方法
    手机号、邮箱、身份证号 格式 验证
    在.cs文件中添加客户端方法
  • 原文地址:https://www.cnblogs.com/godweiyang/p/12203987.html
Copyright © 2011-2022 走看看