zoukankan      html  css  js  c++  java
  • 1118 Backward Digit Sums(数字三角形)

    难度:普及/提高-

    题目类型:DFS

    提交次数:1

    涉及知识: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. 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.

    有这么一个游戏:

    写出一个1~N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少1,直到只剩下一个数字位置。下面是一个例子:

    3 1 2 4

    4 3 6

    7 9 16 最后得到16这样一个数字。

    现在想要倒着玩这样一个游戏,如果知道N,知道最后得到的数字的大小sum,请你求出最初序列a[i],为1~N的一个排列。若答案有多种可能,则输出字典序最小的那一个。

    [color=red]管理员注:本题描述有误,这里字典序指的是1,2,3,4,5,6,7,8,9,10,11,12

    而不是1,10,11,12,2,3,4,5,6,7,8,9[/color]

    输入输出格式

    输入格式:

    两个正整数n,sum。

    输出格式:

    输出包括1行,为字典序最小的那个答案。

    当无解的时候,请什么也不输出。(好奇葩啊)

    代码:

     1 #include<iostream>
     2 using namespace std;
     3 int n, sum;
     4 int a[13][13];
     5 void yanghui(){
     6     int i, j;
     7     a[0][1] = 1;
     8     a[1][1] = 1;
     9     a[1][2] = 1;
    10     for(i = 2; i < n; i++)
    11         for(j = 1; j <= n; j++)
    12             a[i][j] = a[i-1][j]+a[i-1][j-1];
    13     
    14 }
    15 bool flag = 0;
    16 int ans[13];
    17 bool visited[13];
    18 int summ;
    19 void dfs(int step){
    20     if(step == n+1&&summ==sum){
    21         flag = 1;
    22         for(int i = 1; i <= n; i++)
    23             cout<<ans[i]<<" ";
    24         return;
    25     }
    26     if(summ>=sum) return;
    27     if(!flag){
    28         for(int i = 1; i <= n; i++){
    29             if(!visited[i]){
    30                 visited[i] = true;
    31                 ans[step] = i;
    32                 summ += i*a[n-1][step];
    33                 dfs(step+1);
    34                 summ -= i*a[n-1][step];
    35                 visited[i] = false;
    36                 ans[step] = 0;
    37             }
    38         }
    39     }
    40 
    41 }
    42 int main(){
    43     cin>>n>>sum;    
    44     yanghui();
    45     /*for(int i = 0; i < n; i++){
    46         for(int j = 1; j <= i+1; j++)
    47             cout<<a[i][j]<<" ";
    48         cout<<endl;
    49     }*/
    50     dfs(1);
    51     return 0;
    52 }

    备注:

    感觉这道题也很巧妙~

    不管是令人拍案叫绝的递推式,还是从题目抽丝剥茧看到本质,大概都是算法艺术的魅力的冰山一角。

    题解一直在说杨辉三角,可愚钝如我并不知道这有什么联系。直到百度到了一个分析才开窍……杨辉三角就是每个数字加的次数。杨辉三角预处理在a数组中,当step == n+1并且每个数字乘上权值(呈杨辉三角第n-1层分布)等于sum,就可以输出答案了。这时程序的使命已经结束,递归应层层跳出,所以设置了一个flag标记。

    另外有一个小小的减枝。

    弄明白这道题的关键是杨辉三角之后,程序就独立写出来啦,提交一次就过了。

  • 相关阅读:
    struts2第一天——入门和基本操作
    eclipse各种小图标含义
    复制web项目时注意修改web项目名
    AndroidCityPicker仿IOS选择效果
    每日五题(Spring)
    block-循环引用
    给EasyUi的Form加入自己主动填充部分输入框的方法
    智能停车O2O 独角兽初现:“ETCP停车”获5000万美金A轮融资
    解决移动端页面滚动后不触发touchend事件
    《从零開始学Swift》学习笔记(Day 61)——Core Foundation框架之内存管理
  • 原文地址:https://www.cnblogs.com/fangziyuan/p/5936900.html
Copyright © 2011-2022 走看看