数字和为sum的方法数(动态规划)

题目描述

给定一个有n个正整数的数组A和一个整数sum,求选择数组A中部分数字和为sum的方案数。
当两种选取方案有一个数字的下标不一样,我们就认为是不同的组成方案。

输入描述:

输入为两行: 第一行为两个正整数n(1 ≤ n ≤ 1000),sum(1 ≤ sum ≤ 1000) 第二行为n个正整数A[i](32位整数),以空格隔开。

输出描述:

输出所求的方案数

示例1

输入

5 15 5 5 10 2 3

输出

4
 1 import java.util.Scanner;
 2
 3 /**
 4  * @author Dell
 5  *
 6  */
 7 public class Main {
 8 static public long dp[][];
 9 public static void main(String[] args) {
10     Scanner sc = new Scanner(System.in);
11     int n = sc.nextInt();
12     int sum = sc.nextInt();
13     int [] p = new int[(int) (n+1)];
14     /**
15      * 循环全部从1开始
16      *
17      */
18     for (int i= 1; i < p.length; i++) {
19         p[i] = sc.nextInt();
20     }
21     dp = new long[n+1][sum+1];
22     for (int i = 0; i <n+1; i++) {
23         for (int j = 0; j <sum+1; j++) {
24             dp[i][j] = 0;
25         }
26     }
27     dp[0][0] = 1; //初始化 0 行
28     for (int i = 1; i <=n; i++) {
29         for (int j = 0; j <=sum; j++) { // 0-sum列
30             long k;
31             if (j-p[i]<0) {
32                 k = 0;
33             }else {
34                 k = dp[i-1][j-p[i]];
35             }
36             dp[i][j] = dp[i-1][j]+k;
37         }
38     }
39     System.out.println(dp[n][sum]);
40 }
41 }

原文地址:https://www.cnblogs.com/the-wang/p/8981278.html

时间: 2024-08-07 09:36:32

数字和为sum的方法数(动态规划)的相关文章

数字和为sum的方法数

[编程题] 数字和为sum的方法数 给定一个有n个正整数的数组A和一个整数sum,求选择数组A中部分数字和为sum的方案数. 当两种选取方案有一个数字的下标不一样,我们就认为是不同的组成方案. 输入描述: 输入为两行: 第一行为两个正整数n(1 ≤ n ≤ 1000),sum(1 ≤ sum ≤ 1000) 第二行为n个正整数A[i](32位整数),以空格隔开. 输出描述: 输出所求的方案数 输入例子: 5 15 5 5 10 2 3 输出例子: 4方法思想:动态规划思想代码: import j

相加之和为某个数n,求方法数 ------------ 动态规划的方法

uva11137 n3可转化为n2(立方和为n的方法数) /* ID: neverchanje PROG: LANG: C++11 */ #include<iostream> #include<cstring> #include<cstdio> typedef long long ll; using namespace std; int n; long long d[23][maxn]; //下标<=21 int main(){ memset(d,0,sizeof

换钱方法数(有限制条件和无限制条件)

1.  先说一下有限制的 /* 给定一套货币(面值不同的一组货币) vector<int> moeys,以及每种货币的数量vector<int> cnts,求出组合成目标tar的方法数 动态规划思想: dp[i][j]表示用货币moneys[0~i] 组合成 tar 的方法数 dp的规模是:dp[moneys.size()][tar+1] <1> 初始化第一行,dp[0][j], 用moneys[0] 组成j的方法数, moneys[0]只能组成其整数倍的钱数 <

Codeforces 126D Fibonacci Sums 求n由任意的Sum(fib)的方法数 dp

题目链接:点击打开链接 题意: 给定一个数n 问把这个数拆成多个不相同的fibonacci数 有多少种拆法 #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #include<set> #include<queue> #include<vector> #include<m

[程序员代码面试指南]递归和动态规划-机器人达到指定位置方法数(一维DP待做)

题目描述 一行N个位置1到N,机器人初始位置M,机器人可以往左/右走(只能在位置范围内),规定机器人必须走K步,最终到位置P.输入这四个参数,输出机器人可以走的方法数. 解题思路 DP 方法一:时间复杂度O(NK),空间复杂度O(NK) 方法二:时间复杂度O(NK),空间复杂度O(N) 方法一代码 //ans=walk(N,M,K,P); public static int walk(int N,int cur,int rest,int P) { int[][] dp=new int[rest+

APK方法数超过65535及MultiDex解决方案

以下参考自官方文档配置方法数超过 64K 的应用 随着 Android 平台的持续成长,Android 应用的大小也在增加.当您的应用及其引用的库达到特定大小时,您会遇到构建错误,指明您的应用已达到 Android 应用构建架构的极限.早期版本的构建系统按如下方式报告这一错误: Conversion to Dalvik format failed: Unable to execute dex: method ID not in [0, 0xffff]: 65536 较新版本的 Android 构

LeetCode第[91]题(Java):Decode Ways(解码方法数)

题目:解码方法数 难度:Medium 题目内容: A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given a non-empty string containing only digits, determine the total number of ways to decode

彻底解决Android 应用方法数不能超过65K的问题

尊重原创 :http://blog.csdn.net/yuanzeyao/article/details/41809423 作为一名Android开发者,相信你对Android方法数不能超过65K的限制应该有所耳闻,随着应用程序功能不断的丰富,总有一天你会遇到一个异常: Conversion to Dalvik format failed:Unable toexecute dex: method ID not in [0, 0xffff]: 65536 可能有些同学会说,解决这个问题很简单,我们

HDU 2157 How many ways??:矩阵快速幂【i到j共经过k个节点的方法数】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2157 题解: 给你一个有向图,n个节点m条边,问你从i到j共经过k个节点的方法数(不算i点). 题解: 先用邻接矩阵存图. 假设k = 2,那么从i到j的方法数 = ∑ way[i][x] * way[x][j] (0<=x<n && x!=i && x!=j) 诶?快看,那是矩阵乘法! 设邻接矩阵为A,若i到j有边则val[i][j] = 1. k = 2时答案矩