Lecture 59: Python Programming Cases: Shehan King Appreciates Wheat

1.1. Requirements description and design ideas

Description of Requirement:

King Shekhan of India wanted to reward a prime minister and asked him what he wanted:

The prime minister said, please put 1 grain of wheat in the first grid of this chessboard, 2 grains of wheat in the second grid, and 4 grains of wheat in the third grid. The previous grid is twice as large, and it is enough to fill all the 64 grids.

After hearing this, King Shehan thought that the reward was insignificant and he could give it.

The function we need to achieve is to calculate how many grains of wheat King Shekhan needs to put in the 64 grids.

Program design ideas:

  • First of all, we need to clarify the calculation process. There is 1 grain of wheat in the first grid, 2 grains of wheat in the second grid, and 4 grains of wheat in the third grid. It can be seen that the number of wheat in each grid is twice that of the previous grid. The number of wheat in each grid is also 2 to the nth power.
  • The realization of this program is mainly through loops. The number of loops is the number of grids 64. Each loop uses the number of wheat in the grid *2 to get the number of current grids. Finally, add the number of wheat in the grid after each loop. , is the number of wheat characters in all grids.
  • Before starting the loop, define two variables as initial values.
    • The first variable is used to store the number of wheat in the current grid. The first grid has clearly known that there is only 1 wheat.

Guess you like

Origin blog.csdn.net/weixin_44953658/article/details/131281240