hdu2126
分析
太菜了,还是没想到==,好好反省。这微博主讲的已经很到位了,我就写一下留个记录。
http://blog.csdn.net/liuqiyao_01/article/details/8753727
题目
http://acm.hdu.edu.cn/showproblem.php?pid=2126
代码
#include <iostream>
#include <cstring>
#include <ctime>
#include <fstream>
#include <cstdlib>
#include <algorithm>
using namespace std;
int dp[40][2];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=0; i<=m; i++)
{
dp[i][1]=1;
dp[i][0]=0;
}
for(int i=0; i<n; i++)
{
int money;
scanf("%d",&money);
for(int j=m; j>=money; j--)
{
if(dp[j][0]==dp[j-money][0]+1)
{
dp[j][1]=dp[j][1]+dp[j-money][1];
}
else if(dp[j][0]<dp[j-money][0]+1)
{
dp[j][0]=dp[j-money][0]+1;
dp[j][1]=dp[j-money][1];
}
}
}
if(dp[m][0]!=0)
printf("You have %d selection(s) to buy with %d kind(s) of souvenirs.\n",dp[m][1],dp[m][0]);
else
printf("Sorry, you can't buy anything.\n");
}
return 0;
}