http://acm.hdu.edu.cn/showproblem.php?pid=1864

本文探讨了在解决发票背包容量问题时,使用浮点数与双精度数的差异性,通过代码实现并分析了不同数值类型对结果的影响。

不知道这道题用float为什么不行。。double就行郁闷。。。这道题是以发票的张数为背包容量,,从而求出最大价值。。

#include<iostream>
#include<string.h>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
int dp[31];
int s[31];
int main()
{   double w;
    int n;
	int A,B,C;
	while(~scanf("%lf%d",&w,&n),n)
	{  
		memset(dp,0,sizeof(dp));
		memset(s,0,sizeof(s));
		w*=100;
		int tot=0;
		for(int i=0;i<n;++i)
		{
			A=B=C=0;
			int m;
			scanf("%d",&m);
			char ch;
			double a;
			int flag=0;
			while(m--)
			{ getchar();
			 scanf("%c:%lf",&ch,&a);
			 if(ch=='A') A+=a*100;
			 else if(ch=='B') B+=a*100;
			 else if(ch=='C') C+=a*100;
			 else flag=1;
         }
			if(flag||A>60000||B>60000||C>60000||A+B+C>100000||A+B+C>w) flag=1;
			if(!flag)  s[++tot]=A+B+C;
     }
		for(int i=1;i<=tot;++i)
			for(int j=tot;j>=1;--j)
				dp[j]=max(dp[j],dp[j-1]+s[i]);
		int maxx=0;
		  for(int i=1;i<=tot;++i)
		  {
			  if(dp[i]<=w)  maxx=max(maxx,dp[i]);
		  }
		  printf("%.2f\n",maxx/100.00);
	}return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值