前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >2022-03-30:有m个同样的苹果,认为苹果之间无差别,有n个同样的盘子,认为盘子之间也无差别

2022-03-30:有m个同样的苹果,认为苹果之间无差别,有n个同样的盘子,认为盘子之间也无差别

作者头像
福大大架构师每日一题
发布2022-04-13 09:31:15
1310
发布2022-04-13 09:31:15
举报
文章被收录于专栏:福大大架构师每日一题

2022-03-30:有m个同样的苹果,认为苹果之间无差别,

有n个同样的盘子,认为盘子之间也无差别,

还有,比如5个苹果如果放进3个盘子,

那么1、3、1和1、1、3和3、1、1的放置方法,也认为是一种方法。

如上的设定下,返回有多少种放置方法。

答案2022-03-30:

数的分裂。

自然智慧,递归。

代码用golang编写。代码如下:

代码语言:javascript
复制
package main

import "fmt"

func main() {
  ret := ways3(5, 4)
  fmt.Println(ret)
}

var dp [][]int

func ways3(apples, plates int) int {
  if dp == nil {
    dp = make([][]int, 11)
    for i := 0; i < 11; i++ {
      dp[i] = make([]int, 11)
    }
    for i := 0; i <= 10; i++ {
      for j := 0; j <= 10; j++ {
        dp[i][j] = -1
      }
    }
  }
  return process3(apples, plates, dp)
}

func process3(apples, plates int, dp [][]int) int {
  if dp[apples][plates] != -1 {
    return dp[apples][plates]
  }
  ans := 0
  if apples == 0 {
    ans = 1
  } else if plates == 0 {
    ans = 0
  } else if plates > apples {
    ans = process3(apples, apples, dp)
  } else {
    ans = process3(apples, plates-1, dp) + process3(apples-plates, plates, dp)
  }
  dp[apples][plates] = ans
  return ans
}

执行结果如下:

***

[左神java代码](https://github.com/algorithmzuo/weekly-problems/blob/main/src/class_2021_12_4_week/Code05_SplitApples.java)

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-03-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 福大大架构师每日一题 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档