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;
}


你可能感兴趣的:(c,float)