《编程之美》读书随笔之六:饮料供货

题目:在微软亚洲研究院上班,大家早上来的第一件事是干啥呢?查看邮件?No,是去水房拿饮料:酸奶,豆浆,绿茶、王老吉、咖啡、可口可乐……(当然,还是有很多同事把拿饮料当做第二件事)。

管理水房的阿姨们每天都会准备很多的饮料给大家,为了提高服务质量,她们会统计大家对每种饮料的满意度。一段时间后,阿姨们已经有了大批的数据。某天早上,当实习生小飞第一个冲进水房并一次拿了五瓶酸奶、四瓶王老吉、三瓶鲜橙多时,阿姨们逮住了他,要他帮忙。

从阿姨们统计的数据中,小飞可以知道大家对每一种饮料的满意度。阿姨们还告诉小飞,STC(Smart Tea Corp.)负责给研究院供应饮料,每天总量为V。STC很神奇,他们提供的每种饮料之单个容量都是2的方幂,比如王老吉,都是23=8升的,可乐都是25=32升的。当然STC的存货也是有限的,这会是每种饮料购买量的上限。统计数据中用饮料名字、容量、数量、满意度描述每一种饮料。

那么,小飞如何完成这个任务,求出保证最大满意度的购买量呢?

分析:具体见《编程之美》,下面的程序也是根据书上的几个解法来实现的:

 

  1. #include<cstdlib>
  2. #include<string>
  3. #include<iostream>
  4. using namespace std;
  5. #define V 64
  6. #define N 7
  7.  
  8. int opt[V+1][N+1];
  9. int v[N]={2,4,8,2,4,8,16};
  10. int c[N]={3,2,1,3,2,4,1};
  11. int h[N]={20,30,25,30,15,30,100};
  12.  
  13. int cal(int y,int i)
  14. {
  15. if(i==N)
  16. {
  17. if(V==0)
  18. return 0;
  19. else
  20. return -10000;
  21. }
  22. if(V<0)
  23. return -10000;
  24. else if(V==0)
  25. return 0;
  26. else if(opt[V][i]!=-1)
  27. return opt[V][i];
  28. int ret=-1;
  29. for(int t=0;t<=c[t];t++)
  30. {
  31. int temp=cal(V-t*v[t],t+1);
  32. if(temp!=-10000)
  33. {
  34. temp+=h[t]*t;
  35. if(temp>ret)
  36. ret=temp;
  37. }
  38. }
  39. opt[V][i]=ret;
  40. return opt[V][i];
  41. }
  42.  
  43. //备忘录法
  44. void memo_fun()
  45. {
  46. for(int i=0;i<=V;i++)
  47. {
  48. for(int j=0;j<N;j++)
  49. {
  50. opt[i][j]=-1;
  51. }
  52. }
  53. int cc=cal(V,1);
  54. cout<<"biggest happiness is "<<cc<<endl;
  55. }
  56.  
  57. //动态规划法
  58. void dynamic_fun()
  59. {
  60. opt[0][N]=0;
  61. for(int q=0;q<=N;q++)
  62. opt[0][q]=0;
  63. for(int i=1;i<=V;i++)
  64. opt[i][N]=-10000;
  65. for(int j=N;j>=0;j--)
  66. {
  67. for(int i=1;i<=V;i++)
  68. {
  69. opt[i][j]=-10000;
  70. for(int k=0;k<c[j];k++)
  71. {
  72. if(i<k*v[j])
  73. {
  74. break;
  75. }
  76. int x=opt[i-k*v[j]][j+1];
  77. if(x!=-10000)
  78. {
  79. x+=h[j]*k;
  80. if(x>opt[i][j])
  81. {
  82. opt[i][j]=x;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. cout<<"the biggest happiness is: "<<opt[V][0]<<endl;
  89. }
  90.  
  91. int main()
  92. {
  93. cout<<"动态规划算法实现"<<endl;
  94. dynamic_fun();
  95. cout<<"备忘录算法实现"<<endl;
  96. memo_fun();
  97. system("pause");
  98. return 0;
  99. }
posted @ 2019-07-19 12:34  天涯海角路  阅读(122)  评论(0)    收藏  举报