Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python to complete the examination questions for civil servants through dynamic planning

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use Python to complete the civil service examination questions through dynamic planning". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to use Python to complete the civil service examination questions through dynamic planning.

The topics are as follows:

This problem can be done mathematically, but I have been away from school for many years and can't think of a mathematical solution. But the moment I saw the problem, I thought that I could use dynamic programming to solve this problem.

We mark the location of "home" as (0,0) and the location of the unit as (4,3), as shown in the following figure:

A typical solution to dynamic programming is to think backwards when you think about a problem. Suppose I am already at work (4, 3). Where was my last step? There are only two ways to get to (4, 3), from (3, 3) to (4, 3) or from (4, 2) to (4, 3). Now the scale of the problem has shrunk and become two minor problems, one is how many ways to go from home (0,0) to (4,2), and the other is how many ways to go from home (0,0) to (3,3).

At this point, we see that this is actually a recursive problem, that is, fn (x, y) = f (x-1, y) + f (x, y-1).

However, another problem to consider here is when we are in fn (x, 0) or fn (0, y). If x > 1, then there is only one way to do it at this time, that is, from (xmur1, 0) to (x, 0). If x = = 1, it can only be from (0,0) to (1,0) at this time. Similarly, the same is true for (0, y), if y > 1, then only from (0, y-1) to (0, y). If y = = 1, it can only be from (0,0) to (0,1).

So, according to this idea, we can write the following code:

Def find_walk_num (x, y): if y = = 0: if x = = 1: return 1 return find_walk_num (x-1,0) if x = 0: if y = = 1: return 1 return find_walk_num (0, y-1) return find_walk_num (x-1, y) + find_walk_num (x Y-1) result = find_walk_num (4,3) print (f 'from (0,0) to (4,3) has a total of {result})

The running effect is shown in the following figure:

At this point, I believe you have a deeper understanding of "how to use Python to complete the civil service examination questions through dynamic planning". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 276

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report