槐序不是四月吗
2021-06-25 09:12
采纳率: 100%
浏览 597

Python,停车场收费问题

使用Python计算,停车场收费,一小时内免费,超过一小时后第一小时按4块收费,往后每小时收2块,后面不足一小时按一小时算,20块封顶。 求大神~

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • 最佳回答

    分段计算就行

    <span class="hljs-keyword">import</span> math
    x=<span class="hljs-built_in">float</span>(<span class="hljs-built_in">input</span>())
    <span class="hljs-built_in">sum</span>=<span class="hljs-number">0</span>
    <span class="hljs-keyword">if</span> x<=<span class="hljs-number">1</span>:
        <span class="hljs-built_in">sum</span>=<span class="hljs-number">0</span>
    <span class="hljs-keyword">elif</span> x<=<span class="hljs-number">2</span>:
        <span class="hljs-built_in">sum</span>+=<span class="hljs-number">4</span>
    <span class="hljs-keyword">else</span>:
        <span class="hljs-built_in">sum</span>+=<span class="hljs-number">4</span>
        <span class="hljs-built_in">sum</span>+=math.ceil(x-<span class="hljs-number">2</span>)*<span class="hljs-number">2</span>
    <span class="hljs-keyword">if</span> <span class="hljs-built_in">sum</span><=<span class="hljs-number">20</span>:
        <span class="hljs-built_in">print</span>(<span class="hljs-built_in">sum</span>)
    <span class="hljs-keyword">else</span>:
        <span class="hljs-built_in">print</span>(<span class="hljs-number">20</span>)
    

    img

    评论
    解决 2 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题