python活力練習(xí)Day 31
題目:58同城測(cè)試3道編程
題目3:現(xiàn)有一疊鈔票,鈔票由1,5,10,20,50,100這五種類型,小明可以從最上面或者最下面抽取鈔票,每次抽取一張,最多可以抽取5張,求取能抽取出來的最大鈔票數(shù)和。
要求:如果鈔票小于5張,直接計(jì)算所有鈔票的和。
輸入:整型數(shù)組
輸出:最大鈔票和
例:input:[1,5,10,20,50,50,1,2,100,1,1]
output:108
分析1:本題難點(diǎn)在于輸入的是字符串形式,首先要將字符串轉(zhuǎn)換為整形數(shù)組。此處利用數(shù)據(jù)結(jié)構(gòu)的雙指針?biāo)枷雭磉M(jìn)行
1 s = input()[1:-1] + ',' 2 result = [] 3 i = 0 4 while i < len(s) - 1: 5 j = i+1 6 while s[i] != "," and s[j] != ",": 7 j += 1 8 result.append(s[i:j]) 9 print(result)
分析2:可以進(jìn)行組合[2+2+1]/[3+2|2+3]/[1+4|4+1]/[5|5]
1 s = result 2 max_1 = sum(s[:2]) + sum(s[-2:]) + max(s[3],s[-3]) 3 max_2 = max(sum(s[:3]) + sum(s[-2:]),sum(s[:2]) + sum(s[-3:])) 4 max_3 = max(sum(s[:4]) + sum(s[-1:]),sum(s[:1]) + sum(s[-4:])) 5 max_4 = max(sum(s[:5],sum(s[-5:]))) 6 print(max(max_1,max_2,max_3,max_4))
posted on 2020-09-15 09:17 dangdangA 閱讀(144) 評(píng)論(0) 收藏 舉報(bào)
浙公網(wǎng)安備 33010602011771號(hào)