Pupils Blue Bridge Cup Python Breakthrough | A secret room with poisonous gas

Learn Python from a baby! Record the questions in the Blue Bridge Cup Python learning and test preparation process, and record every moment.

Attached is a summary post: Pupils Blue Bridge Cup Python Breakthrough | Summary_COCOgsta's Blog-CSDN Blog


【Description】

There is an escape room game with 100 rooms in a row. The secret rooms are numbered consecutively from 1 to the 100th secret room, as shown in the figure below:

game rules:

The player's initial position is in Room 1;

Each time the player can enter a secret room on the right, or skip a secret room to enter the next secret room (for example: when the player is currently in No. 3 secret room, he can enter No. 4 secret room or No. 5 secret room);

The secret room with poisonous gas cannot be entered and needs to be avoided.

Given three positive integers X, Y, M (X<Y<M≤100), represent the numbers of three secret rooms. No. X secret room and No. Y secret room have poisonous gas leakage and cannot be entered. Players need to enter No. M secret room. According to the rules of the game, how many routes are there to enter the secret room of M.

For example: X=2, Y=4, M=7, there are 2 routes to enter the secret room M, namely 1->3->5->6->7 and 1->3->5-> 7 routes.

【Enter description】

Enter three positive integers X, Y, M (X<Y<M), X and Y represent the number of the toxic airlock, M represents the number of the chamber to be entered, and the three positive integers are separated by commas

【Output description】

Output how many route plans there are to enter the secret room M

【Sample input】

2,4,7

【Sample output】

2

【Code Explanation】

#f(n) = f(n-1) + f(n-2)

x,y,m = [int(i) for i in input().split(",")]
f = [0, 1]
for i in range(2, m+1):
    if i == x or i == y:
        f.append(0)
    else:
        f.append(f[i-1]+f[i-2])

print(f[-1])
复制代码

【operation result】

2,4,7
2

 

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/130469958