设为首页 加入收藏

TOP

8.9.3 实现CBox类(10)
2013-10-07 16:10:03 来源: 作者: 【 】 浏览:74
Tags:8.9.3 实现 CBox

8.9.3  实现CBox类(10)

该问题的第二部分是使用3个嵌套的for循环解决的。这些循环在m_Length、m_Width和m_Height的允许范围内迭代,从而评估所有可能的组合。可以将这些组合全部输出到屏幕上,但因为涉及的组合多达200个,而我们可能只对其中一小部分感兴趣,所以程序中使用一条if语句来识别那些我们感兴趣的选项。仅当纸板箱中没有浪费的空间、当前试验的糖果盒中没有浪费的空间,并且该糖果盒至少包含30块糖果时,if表达式才是true。

示例说明

该程序的输出如下:

  1. There are 42 candies per candy box  
  2. For the standard boxes there are 144 candy boxes per carton  
  3. with 648 cubic inches wasted.  
  4. CUSTOM CANDY BOX ANALYSIS (No Waste)  
  5. Trial Box L = 5 W = 4.5 H = 2 
  6. Trial Box contains 30 candies and a carton contains 216 candy boxes.  
  7. Trial Box L = 5 W = 4.5 H = 2 
  8. Trial Box contains 30 candies and a carton contains 216 candy boxes.  
  9. Trial Box L = 6 W = 3 H = 2.5  
  10. Trial Box contains 30 candies and a carton contains 216 candy boxes.  
  11. Trial Box L = 6 W = 4.5 H = 2 
  12. Trial Box contains 36 candies and a carton contains 180 candy boxes.  
  13. Trial Box L = 6 W = 4.5 H = 2.5  
  14. Trial Box contains 45 candies and a carton contains 144 candy boxes.  
  15. Trial Box L = 6 W = 5 H = 1.5  
  16. Trial Box contains 30 candies and a carton contains 216 candy boxes.  
  17. Trial Box L = 6 W = 5 H = 2 
  18. Trial Box contains 40 candies and a carton contains 162 candy boxes.  
  19. Trial Box L = 6 W = 5 H = 2.5  
  20. Trial Box contains 50 candies and a carton contains 129 candy boxes.  
  21. Trial Box L = 7.5 W = 3 H = 2 
  22. Trial Box contains 30 candies and a carton contains 216 candy boxes.  

因为嵌套循环中既评估长5英寸、宽4.5英寸的箱子,也评估长4.5英寸、宽5英寸的箱子,所以得到一个重复的解决方案。因为CBox类的构造函数能够确保长度不小于宽度,所以这两个答案是相同的。可以包括一些避免出现重复方案的其他逻辑,但这样做几乎没有效果。如果愿意,那么您可以将其当作一道练习题试一下。
 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇8.9.3 实现CBox类(9) 下一篇8.10 组织程序代码

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·python数据分析岗的 (2025-12-25 10:02:21)
·python做数据分析需 (2025-12-25 10:02:19)
·成为一个优秀的pytho (2025-12-25 10:02:16)
·Java后端面试实习自 (2025-12-25 09:24:21)
·Java LTS版本有哪些 (2025-12-25 09:24:18)