import java.util.Scanner;
class test{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int[] price = new int[n+1];//price[i]:表示用第i种菜的价格
int[][] res = new int[n+1][m+1];//res[i][j]:表示用j元吃i种菜的方案数
for (int i = 1 ; i <= n ; i++)
{
price[i] = sc.nextInt();
}
for(int i = 1 ; i <= n ; i++)
{
for (int j = 1 ; j <= m ; j++)
{
/*
三种情况:
1. 目前有的钱和第i道菜的价格相同
2. 当前的钱充足
3. 当前的钱不足
*/
if (j == price[i])
{
res[i][j] = res[i-1][j] + 1;
//当前的钱和第i道菜的价格相同
//吃(吃的话,则为1,因为只能吃这一道菜)
//不吃, res[i-1][j] ,则是用j吃前边的i-1道菜的方案数。
}
if (j > price[i])
{
res[i][j] = res[i-1][j] + res[i-1][j-price[i]];
//若钱充足,吃 or 不吃
}
if (j < price[i])
{
res[i][j] = res[i-1][j];
//若钱不足,只能不吃
}
}
}
System.out.println(res[n][m]);
}
}
洛谷 P1164 小A点菜 Java
猜你喜欢
转载自blog.csdn.net/qq_45260619/article/details/105512266
今日推荐
周排行