Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commiteb51c41

Browse files
零钱找零 美团笔试题
1 parent3e5e264 commiteb51c41

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

‎Target Offer/零钱找零以及进阶

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'''
2+
零钱找零问题,使用动态规划
3+
'''
4+
def ChangeMaking(coinVal, change):
5+
alist = [0]*(change+1)
6+
for i in range(1, change+1):
7+
temp = change; j = 0
8+
while j <= len(coinVal)-1 and i >= coinVal[j]:
9+
temp = min(alist[i-coinVal[j]], temp)
10+
j += 1
11+
alist[i] = temp + 1
12+
return alist.pop()
13+
14+
print(ChangeMaking([1, 5, 10, 25], 63))
15+
16+
'''
17+
零钱找零问题的进阶
18+
美团笔试题
19+
给你六中零钱1,5,10,20,50,100的纸币,给定一个金额,写出所有可能的找零的个数
20+
输入2,输出1;输入5,输出2
21+
也是使用动态规划
22+
'''
23+
import sys
24+
try:
25+
while True:
26+
line = sys.stdin.readline().strip()
27+
if line == '':
28+
break
29+
target = int(line)
30+
coinVal = [1, 5, 10, 20, 50, 100]
31+
alist = [0]*(target+1)
32+
alist[0] = 1
33+
for i in range(6):
34+
j = coinVal[i]
35+
while j <= target:
36+
alist[j] = alist[j] + alist[j-coinVal[i]]
37+
j += 1
38+
print(alist[-1])
39+
except:
40+
pass

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp