美文网首页
coin change

coin change

作者: luomoxyz | 来源:发表于2016-12-07 11:37 被阅读27次

    最简单的DP

    package main
    import "fmt"
    
    func main() {
     //Enter your code here. Read input from STDIN. Print output to STDOUT
        var sum int
        var num int
        fmt.Scanln(&sum, &num)
        arr := make([]int, 0, num)
        for i := 0; i < num; i++ {
            var t int
            fmt.Scanf("%d", &t)
            arr = append(arr, t)
        }
        //var result int
        //recurit(arr, sum, &result)
    
        var dp = make([]int, sum+1)
        dp[0] = 1
        for i := 0; i < num; i++ {
            var coin = arr[i]
            for j := coin; j < len(dp); j++ {
                dp[j] += dp[j-coin]
            }
        }
        fmt.Println(dp[sum])
    }
    

    相关文章

      网友评论

          本文标题:coin change

          本文链接:https://www.haomeiwen.com/subject/uxpdmttx.html