2021-06-25 09:12

# Python，停车场收费问题

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

#### 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>)
``````

评论
解决 2 无用
打赏 举报