HDU 1248 寒冰王座

完全背包问题或者直接贪心

题目链接HDU 1248

题目

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Problem Description
不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,只有一张钞票),为了防止自己在战斗中频繁的死掉,他决定给自己买一些道具,于是他来到了地精商店前.
死亡骑士:”我要买道具!”
地精商人:”我们这里有三种道具,血瓶150块一个,魔法药200块一个,无敌药水350块一个.”
死亡骑士:”好的,给我一个血瓶.”
说完他掏出那张N元的大钞递给地精商人.
地精商人:”我忘了提醒你了,我们这里没有找客人钱的习惯的,多的钱我们都当小费收了的,嘿嘿.”
死亡骑士:”……”
死亡骑士想,与其把钱当小费送个他还不如自己多买一点道具,反正以后都要买的,早点买了放在家里也好,但是要尽量少让他赚小费.
现在死亡骑士希望你能帮他计算一下,最少他要给地精商人多少小费.

Input
输入数据的第一行是一个整数T(1<=T<=100),代表测试数据的数量.然后是T行测试数据,每个测试数据只包含一个正整数N(1<=N<=10000),N代表死亡骑士手中钞票的面值.
注意:地精商店只有题中描述的三种道具.

Output
对于每组测试数据,请你输出死亡骑士最少要浪费多少钱给地精商人作为小费.

Sample Input
2
900
250

Sample Output
0
50

完全背包AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <bitset>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
int value[4]={0,150,200,350};
int dp[10010];
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
memset(dp,0,sizeof(dp));
for(int i=1;i<=3;i++)
{
for(int j=value[i];j<=n;j++)
{
if(dp[j]<=dp[j-value[i]]+value[i])
{
dp[j]=dp[j-value[i]]+value[i];
}
}
}
cout<<n-dp[n]<<endl;
}
return 0;
}

贪心AC代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*
题解:350为150和200的和,所以可以不买350的,用150和200的药水代替即可。
然后150和200又可以拆成150+0和150+50,首先看能买多少150的药水
然后可以在每瓶的基础上加50元或者不加,这样就能留下最少的小费给地精商人
(PS:地精连死亡骑士的小费都敢收,不怕被砍?)
*/
#include<iostream>
using namespace std;
int main(void)
{
int t,n;
cin>>t;
while(t--)
{
cin>>n;
int sum=n;
int s1=sum/150;
sum=sum%150;
if(sum/50<=s1)
{
sum=sum%50;
}
else
{
sum=sum-(50*s1);
}
cout<<sum<<endl;
}
return 0;
}
坚持原创技术分享,您的支持将鼓励我继续创作!