zoukankan      html  css  js  c++  java
  • python活力练习Day 31

    题目:58同城测试3道编程

    题目3:现有一叠钞票,钞票由1,5,10,20,50,100这五种类型,小明可以从最上面或者最下面抽取钞票,每次抽取一张,最多可以抽取5张,求取能抽取出来的最大钞票数和。

    要求:如果钞票小于5张,直接计算所有钞票的和。

    输入:整型数组

    输出:最大钞票和

    例:input:[1,5,10,20,50,50,1,2,100,1,1]

      output:108


    分析1:本题难点在于输入的是字符串形式,首先要将字符串转换为整形数组。此处利用数据结构的双指针思想来进行

    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:可以进行组合[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))
  • 相关阅读:
    【Struts 动态表单】DynaActionForm
    【Struts 分派Action】DispatchAction
    【struts 报错】 No action config found for the specified url
    【Struts APP_PATH】StartSystemListener
    【Struts 编码】
    【Struts 基础案例】
    28. 实现 strStr()
    14. 最长公共前缀
    2. 两数相加
    15. 三数之和
  • 原文地址:https://www.cnblogs.com/xiaodangdang/p/13671277.html
Copyright © 2011-2022 走看看