程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python monkeys steal peaches

編輯:Python

One 、 Monkeys steal peaches

The monkey picked some peaches on the first day , Half eaten immediately , Not yet , Another one . The next morning, he ate half of the remaining peaches and another one , After that, I ate half and one of the rest of the day before every morning . To the first 10 When you want to eat again in the morning , There's only one peach left . Please pick how many peaches on the first day ?

Two 、 analysis

Reverse reasoning
Set the first 9 There is x Peach :
x - x / 2 - 1=1
be x=4
The code is

x = (x + 1) * 2

3、 ... and 、 Code

x = 1
for i in range(9):
x = (x + 1) * 2
print(" The first ", 9 - i,
" There are still peaches left before eating ", x, " individual ")

Four 、 Output results

 The first 9 There are still peaches left before eating 4 individual
The first 8 There are still peaches left before eating 10 individual
The first 7 There are still peaches left before eating 22 individual
The first 6 There are still peaches left before eating 46 individual
The first 5 There are still peaches left before eating 94 individual
The first 4 There are still peaches left before eating 190 individual
The first 3 There are still peaches left before eating 382 individual
The first 2 There are still peaches left before eating 766 individual
The first 1 There are still peaches left before eating 1534 individual

5、 ... and 、 communication

If you have a better solution or have different opinions , You can speak freely below , Jointly improve progress !
I'm also Xiaobai , Zero base learning python, Dare to ask questions , Let's discuss , Learn together and make progress !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved