解题思路
相关代码
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
int M = scanner.nextInt();
int a[] = new int[N+1];
for(int i=1;i<=N;i++) a[i] = scanner.nextInt();
int f[][] = new int[N+1][M+1];
//f[i][j]是方案数,一般是需要对f[0][0]进行初始化
f[0][0]=1;
for(int i=1;i<=N;i++)
for(int j=0;j<=M;j++){
f[i][j]=f[i-1][j];
if(j>=a[i]) f[i][j]=f[i][j]+f[i-1][j-a[i]];
}
System.out.println(f[N][M]);
}
}