码迷,mamicode.com
首页 > 其他好文 > 详细

Backward Digit Sums POJ - 3187

时间:2019-07-16 18:53:20      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:from   nal   hint   output   puts   ret   least   lang   eterm   

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.
 
百度翻译:

FJ和他的牛喜欢玩心理游戏。他们将1到n(1<=n<=10)之间的数字按一定的顺序写下来,然后对相邻的数字求和,生成一个新的列表,其中少一个数字。它们重复这个过程,直到只剩下一个数字。例如,游戏的一个实例(n=4)可能如下所示:

3 1 2 4

4 3 6

7 9

16

在FJ的背后,奶牛们开始玩一个更困难的游戏,在游戏中他们试图从最后的总数和数字N中确定开始的顺序。不幸的是,游戏有点高于FJ的心理算术能力。写一个程序来帮助FJ玩游戏,跟上牛的步伐。

思路:一个简单的全排列(next_permutation)就行了,全排列按字典序列从小到大,直到找到满足条件的就输出。注意不要超时。

 1 #include <cstdio>
 2 #include <fstream>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <deque>
 6 #include <vector>
 7 #include <queue>
 8 #include <string>
 9 #include <cstring>
10 #include <map>
11 #include <stack>
12 #include <set>
13 #include <sstream>
14 #include <iostream>
15 #define mod 1000000007
16 #define eps 1e-6
17 #define ll long long
18 #define INF 0x3f3f3f3f
19 using namespace std;
20 
21 int n,he;
22 int sz[15];
23 int qh()
24 {
25     int s=n;
26     int num[15];
27     for(int i=0;i<s;i++)
28     {
29         num[i]=sz[i];
30     }
31     while(s!=1)
32     {
33         for(int i=0;i<s-1;i++)
34         {
35             num[i]=num[i]+num[i+1];
36         }
37         s--;
38     }
39     return num[0];
40 }
41 int main()
42 {
43     scanf("%d %d",&n,&he);
44     for(int i=0;i<n;i++)
45     {
46         sz[i]=i+1;
47     }
48     do
49     {
50        if(qh()==he)
51        {
52             for(int i=0;i<n-1;i++)
53             {
54                 printf("%d ",sz[i]);
55             }
56             printf("%d\n",sz[n-1]);
57            break;
58        }
59     }while(next_permutation(sz,sz+n));
60 
61 }

 

Backward Digit Sums POJ - 3187

标签:from   nal   hint   output   puts   ret   least   lang   eterm   

原文地址:https://www.cnblogs.com/mzchuan/p/11196495.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!