【POJ - 3187】Backward Digit Sums(搜索)

-->Backward Digit Sums

直接写中文了

Descriptions:

FJ 和 他的奶牛们在玩一个心理游戏。他们以某种方式写下1至N的数字(1<=N<=10)。 然后把相邻的数相加的到新的一行数。重复这一操作直至只剩一个数字。比如下面是N=4时的一种例子

    3   1   2   4
      4   3   6
        7   9
         16

在FJ回来之前,奶牛们开始了一个更难的游戏:他们尝试根据最后结果找到开始的序列。这已超过了FJ的思考极限。

写一个程序来帮助FJ吧


Input

N和最后的和


Output

满足要求的一行序列。若有多种情况,输出字典序最小的一种


Sample Input

4 16

Sample Output

3 1 2 4

Hint

样例解释

这里还有其他可能的排列,若 3 2 1 4,但 3 1 2 4 是字典序最小的

题目链接:

https://vjudge.net/problem/POJ-3187

直接对这n个数进行全排列,然后对其求和,比对答案即可

既然是全排列 这里有一个全排列函数,可以直接调用,而且排列顺序就是字典顺序,很方便  next_permutation 参考:https://www.cnblogs.com/sky-stars/p/10948384.html 讲的很细

AC代码:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 1005
using namespace std;
int a[Maxn];
int n;
int ans,result;
void solve()
{
    do
    {
        int b[Maxn];//注意一定要设置一个新数组,不然a数组会混乱
        for(int i=0;i<n;i++)
            b[i]=a[i];
        for(int i=n; i>=2; i--)//求出这组数组的和
        {
            for(int j=0; j<i; j++)
            {
                b[j]=b[j]+b[j+1];
            }
        }
        result=b[0];//b[0]即是这组数字之和
        if(result==ans)//得到正确答案,输出数组即可
        {
            for(int i=0; i<n; i++)
            {
                cout<<a[i]<<" ";
            }
            cout<<endl;
            break;
        }
    }
    while(next_permutation(a,a+n));//全排列
}
int main()
{
    cin>>n>>ans;
    for(int i=0; i<n; i++)//输入数组
        a[i]=i+1;
    solve();
}

原文地址:https://www.cnblogs.com/sky-stars/p/11191951.html

时间: 2024-11-11 12:10:02

【POJ - 3187】Backward Digit Sums(搜索)的相关文章

POJ 3187 Backward Digit Sums(next_permutation)

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

POJ 3187 Backward Digit Sums 题解 《挑战程序设计竞赛》

题目:POJ 3187 思路: 这道题很简单,用next_permutation枚举1~N的所有排列,然后依次相加,判断最后的和是否等于sum,是的话则break,即为字典序最前的. 1 #include <iostream> 2 #include <algorithm> 3 4 using namespace std; 5 6 int n; 7 int sum; 8 int mycase[11][11]; 9 10 int main() { 11 cin >> n &

POJ 3187 Backward Digit Sums (杨辉三角,穷竭搜索,组合数,DFS)

http://poj.org/problem?id=3187 将一行数按杨辉三角的规则计算为一个数,已知最后那个数和三角形的高度,求最初的那行数. 杨辉三角前10行:                   1                                   1   1                               1   2   1                           1   3   3   1                       1  

poj 3187 Backward Digit Sums(穷竭搜索dfs)

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

POJ 3187 Backward Digit Sums

http://poj.org/problem?id=3187 穷竭搜索 全排列 然后按规则求和 排列之前先按升序排序 这样可以保证第一个和为k的就是符合最小序列的结果 1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 5 using namespace std; 6 7 int sum(int a[], int n) 8 { 9 int s[11]; 10 for (int i = 0

POJ 3187 Backward Digit Sums (dfs,杨辉三角形性质)

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. Fo

POJ 题目Backward Digit Sums(next_permutation)

Backward Digit Sums Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4807   Accepted: 2772 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 ad

BZOJ 1653 [Usaco2006 Feb]Backward Digit Sums ——搜索

[题目分析] 劳逸结合好了. 杨辉三角+暴搜. [代码] #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <map> #include <set> #include <queue> #include <string> #include <iostream> #include <a

bzoj1653:Backward Digit Sums

1653: [Usaco2006 Feb]Backward Digit Sums Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 318  Solved: 239[Submit][Status][Discuss] Description FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a cer