ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Backward Digit Sums POJ - 3187

2019-07-16 18:02:36  阅读:188  来源: 互联网

标签:Digit Backward 16 int game POJ FJ include define


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 }

 

标签:Digit,Backward,16,int,game,POJ,FJ,include,define
来源: https://www.cnblogs.com/mzchuan/p/11196495.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有