首页 技术 正文
技术 2022年11月16日
0 收藏 814 点赞 4,558 浏览 1349 个字

饭卡: 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额。如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够)。所以大家都希望尽量使卡上的余额最少。 
某天,食堂中有n种菜出售,每种菜可购买一次。已知每种菜的价格以及卡上的余额,问最少可使卡上的余额为多少。 

Input多组数据。对于每组数据: 
第一行为正整数n,表示菜的数量。n<=1000。 
第二行包括n个正整数,表示每种菜的价格。价格不超过50。 
第三行包括一个正整数m,表示卡上的余额。m<=1000。

n=0表示数据结束。 
Output对于每组输入,输出一行,包含一个整数,表示卡上可能的最小余额。Sample Input

1
50
5
10
1 2 3 2 1 1 2 3 2 1
50
0

Sample Output

-45
32
#include <stdio.h>
#include<iostream>
#include <algorithm>
using namespace std;
int cmp(int a,int b){return a<b;}
int main()
{
int n;
while(~scanf("%d",&n),n)
{
int i,price[]= {},dp[] = {};
for(i = ; i<=n; i++)
scanf("%d",&price[i]);
sort(price+,price++n,cmp);
int MAX=price[n];
int j,m;
scanf("%d",&m);
if(m<)//低于5元不能购买
{
printf("%d\n",m);
continue;
}
m-=;//取出5元用于购买最贵的物品
for(int i = ; i<n; i++)//01背包
{
for(int j = m;j>=price[i];j--)
{
dp[j] = max(dp[j],dp[j-price[i]]+price[i]);
//cout<<i<<": "<<"dp"<<j<<": "<<dp[j]<<endl;
}
}
printf("%d\n",m+-dp[m]-MAX);
}
return ;
}

http://www.cppblog.com/tanky-woo/archive/2010/07/31/121803.html

#include<iostream>
#define N 1010
#include<algorithm>
#include<string.h>
#include<stdio.h>
int pri[N];
int m,n;
int dp[N];
using namespace std;
int main(){
while(scanf("%d",&n)&&n!=){
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++){
scanf("%d",&pri[i]);
}
scanf("%d",&m);
sort(pri+,pri+n+);
if(m<){
printf("%d\n",m);
continue;
}
for(int i=;i<n;i++){
for(int j=m-;j>=pri[i];j--){
dp[j]=max(dp[j],dp[j-pri[i]]+pri[i]);
}
}
printf("%d\n",m-dp[m-]-pri[n]);
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898