本题也是一个背包的问题,我觉得这道题的核心就是根据精确度将浮点型转化为整型然后利用动态规划进行求解,注意对题意的理解,有3种支票是不能够报销的。

我开始照着这个思路进行思考,但是敲出来的第一个代码居然超时了,想了一下没想出来就看了一下别人的代码,感觉上没什么才差别,但是别人的却a掉了。。。希望有哪位朋友能够指点一下。

超时的代码:

#include"iostream"
#include"stdio.h"
#include"cmath"
#include"string.h"
#include"algorithm"
#define mx 10005
using  namespace std;
int dp[5000024],flag,b[34];
char type;
double money;
int main()
{
    double m,suma,sumb,sumc,sum;
    int i,j,n,num,k;
    while(cin>>m>>n,n)
    {
        int count=0;
        int x=(int)(m*100);
        for(i=1;i<=n;i++)
        {
            cin>>num;
            suma=sumb=sumc=sum=0;
            getchar();
            flag=0;
            for(j=1;j<=num;j++)
            {
                cin>>type;
                getchar();
                cin>>money;
                if(j<num) getchar();
                money=(int)(money*100);
                if(!flag){
                if(type=='A')
                {
                    suma+=money;
                    if(suma>60000) {flag=1;break;}
                }
                else if(type=='B')
                {
                    sumb+=money;
                    if(sumb>60000) {flag=1;break;}
                }

                else if(type=='C')
                {
                    sumc+=money;
                    if(sumc>60000) {flag=1;break;}
                }
                else {flag=1;break;}

                }
           }
        sum=suma+sumb+sumc;
        if(!flag&&sum<=100000) b[count++]=sum;
    }
     memset(dp,0,sizeof(dp));
        for(i=0;i<=count;i++)
        {
            for(k=x;k>=b[i];k--)
            {
                if(dp[k]<dp[k-b[i]]+b[i])
                    dp[k]=dp[k-b[i]]+b[i];
            }
        }
     printf("%.2lf\n",dp[x]/100.0);
    }
    return 0;
}
View Code

相关文章:

  • 2021-10-15
  • 2022-12-23
  • 2022-12-23
  • 2021-06-23
  • 2021-10-01
  • 2022-12-23
猜你喜欢
  • 2021-12-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-02-04
  • 2021-07-07
相关资源
相似解决方案