Python實現make_bricks與make_chocolate問題
Python實現make_bricks與make_chocolate問題
問題描述
1:make_bricks
We want to make a row of bricks that is goal inches long. We have a number of small bricks (1 inch each) and big bricks (5 inches each). Return True if it is possible to make the goal by choosing from the given bricks. This is a little harder than it looks and can be done without any loops. See also: Introduction to MakeBricks
make_bricks(3, 1, 8) → True
make_bricks(3, 1, 9) → False
make_bricks(3, 2, 10) → True
2:make_chocolate
We want make a package of goal kilos of chocolate. We have small bars (1 kilo each) and big bars (5 kilos each). Return the number of small bars to use, assuming we always use big bars before small bars. Return -1 if it can’t be done.
make_chocolate(4, 1, 9) → 4
make_chocolate(4, 1, 10) → -1
make_chocolate(4, 1, 7) → 2
解決思想
use big bars before small bars(儘可能的使用長的)
實現程式碼
def make_bricks(small, big, goal):
if 5big<goal:
s = goal-5big
if s<=small:
return True
else:
s = goal%5
return s<=small
return False
測試結果:
def make_chocolate(small, big, goal):
if 5big<goal:
s = goal-5big
if s<=small:
return s
else:
s = goal%5
return (s if(s<=small) else -1)
return -1
測試結果:
相關文章
- Python實現:漢諾塔問題Python
- 實現模式--技能與如何解決問題模式
- 梯度下降法實現最簡單線性迴歸問題python實現梯度Python
- 圖論最短路徑問題與matlab實現圖論Matlab
- 迴圈連結串列(約瑟夫問題)--python實現Python
- Python yield與實現Python
- 您說的這個功能實現不是問題,問題是實現不了~~
- 傳教士與食人者問題pythonPython
- Python與Django的時區問題PythonDjango
- 3.Redis實現問題Redis
- python實現Dijkstra演算法之 最短路徑問題Python演算法
- 【經驗分享】Python實現UI自動化難點問題PythonUI
- 最短路徑問題,BFS,408方向,思路與實現分析
- Python實現火柴人的設計與實現Python
- DS2500 Python實踐問題Python
- Python的requests庫:解決文件缺失問題的策略與實踐Python
- 八皇后問題分析和實現
- tcp 實現簡單http 問題TCPHTTP
- 漢羅塔問題 java實現Java
- CSS實現垂直居中的問題CSS
- Java實現-揹包問題IJava
- Java實現-揹包問題IIJava
- Java實現-揹包問題VIJava
- 讀寫者問題-java實現Java
- 遞迴實現漢諾塔問題遞迴
- 問題多多的STL實現 (轉)
- input函式出現的問題(Python)函式Python
- 百練OJ:4147:漢諾塔問題(Hanoi)——python實現漢諾塔Python
- vue---axios實現資料互動與跨域問題VueiOS跨域
- RSA演算法與Python實現演算法Python
- Python實現堆疊與佇列Python佇列
- Python實現微博輿情分析的設計與實現Python
- Python程式設計常見問題與解答Python程式設計
- Python 疑難問題:[] 與 list() 哪個快?Python
- URL重寫(rewrite)的具體實現與異常問題解決
- [全程建模]UML設計類中的實現與方法數量問題
- python 3.6.2 安裝與執行 Scrapy 問題與解決Python
- 深度學習入門:基於Python的理論與實現-第三章sys.path問題深度學習Python