当前位置:网站首页>Day5: construct program logic
Day5: construct program logic
2022-07-24 11:51:00 【Sumarua】
Construct program logic
After learning the previous chapters , I think it is necessary to do some exercises here to consolidate what I have learned before , Although what we have learned so far is only Python The tip of the iceberg , But that's enough for us to build the logic in the program . For beginners of programming languages , In the study Python Core language elements of ( Variable 、 type 、 Operator 、 expression 、 Branching structure 、 Circulation structure, etc ) after , One of the things that must be done is to try to use what we have learned to solve real problems , In other words, it is to exercise the algorithm described in human natural language ( Methods and steps to solve the problem ) Translate into Python The power of code , And this has to be done through a lot of practice .
In this chapter, we have sorted out some classic cases and exercises for you , I hope that through these examples , On the one hand, it helps us consolidate what we have learned before Python knowledge , On the other hand, it helps us understand how to establish the logic in the program and how to use some simple algorithms to solve real problems .
Classic example
seek Narcissistic number .
explain : Narcissus number is also called super perfect number invariant 、 Narcissistic number 、 Self idempotent 、 Armstrong number , It's a 3 digit , The sum of the cubes of each digit of the number is exactly equal to itself , for example : 1 3 + 5 3 + 3 3 = 153 1^3 + 5^3+ 3^3=153 13+53+33=153.
""" Find out the number of all daffodils """ for num in range(100, 1000): low = num % 10 mid = num // 10 % 10 high = num // 100 if num == low ** 3 + mid ** 3 + high ** 3: print(num)In the code above , We find out the single digit of a three digit number by integer division and modulo operation 、 Ten and a hundred , This little technique is still commonly used in actual development . In a similar way , We can also reverse a positive integer , for example : take 12345 become 54321, The code is as follows .
""" The inverse of a positive integer """ num = int(input('num = ')) reversed_num = 0 while num > 0: reversed_num = reversed_num * 10 + num % 10 num //= 10 print(reversed_num)A hundred money and a hundred chickens problem .
explain : A hundred money and a hundred chickens are ancient mathematicians in China Qiu Jian Zhang stay 《 Sutra 》 A mathematical problem raised in a book : A chicken is worth five , A hen is worth three , A chicken is worth three . A hundred dollars for a hundred chickens , Ask jiweng 、 Hen 、 Every chicken has its own geometry ? The translation into modern Chinese is : cock 5 One yuan , hen 3 One yuan , Chick 1 Three yuan , use 100 A hundred chickens for $1 , Ask the rooster 、 hen 、 How many chickens are there each ?
""" 《 A hundred money and a hundred chickens 》 problem """ for x in range(0, 20): for y in range(0, 33): z = 100 - x - y if 5 * x + 3 * y + z / 3 == 100: print(' cock : %d only , hen : %d only , Chick : %d only ' % (x, y, z))The method used above is called Exhaustive method , Also known as Violent search , This method enumerates all possible candidates of alternative solutions one by one and checks whether each candidate conforms to the description of the problem , Finally, the solution of the problem is obtained . This method looks clumsy , But for computers with very powerful computing power , It is usually a feasible or even good choice , And if the solution of the problem exists , This method must be able to find it .
CRAPS Gambling games .
explain :CRAPS Also known as Citi dice , It is a very popular table gambling game in Las Vegas . The game uses two dice , Players get points by rolling two dice to play the game . The simple rule is : If the player shakes the dice for the first time 7 Point or 11 spot , Players win ; If the player shakes out for the first time 2 spot 、3 Point or 12 spot , Chuang Jiasheng ; Other players continue to roll dice , If the player shakes out 7 spot , Chuang Jiasheng ; If the player shakes the points for the first time , Players win ; Other points , Players continue to want dice , Until the winner is decided .
""" Craps Gambling games We set the player to start the game with 1000 A bet of $ The condition for the end of the game is that the player loses all his bets """ from random import randint money = 1000 while money > 0: print(' Your total assets are :', money) needs_go_on = False while True: debt = int(input(' Please bet : ')) if 0 < debt <= money: break first = randint(1, 6) + randint(1, 6) print(' The player shakes out %d spot ' % first) if first == 7 or first == 11: print(' Players win !') money += debt elif first == 2 or first == 3 or first == 12: print(' Chuang Jiasheng !') money -= debt else: needs_go_on = True while needs_go_on: needs_go_on = False current = randint(1, 6) + randint(1, 6) print(' The player shakes out %d spot ' % current) if current == 7: print(' Chuang Jiasheng ') money -= debt elif current == first: print(' Players win ') money += debt else: needs_go_on = True print(' You're broke , Game over !')
### Useful exercises
Generate Fibonacci sequence Before 20 Number .
explain : Fibonacci sequence (Fibonacci sequence), Also called golden section series , It's Leonardo, an Italian mathematician · Fibonacci (Leonardoda Fibonacci) stay 《 Book of calculation 》 In this paper, we propose a series of numbers for the growth rate of rabbit under ideal hypothesis , So this sequence is also called " Rabbit Series ". The characteristic of Fibonacci series is that the first two numbers of the series are 1, Start with the third number , Each number is the sum of its first two numbers , Form like :1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …. Fibonacci series in modern physics 、 Quasicrystal structure 、 There are direct applications in chemistry and other fields .
find 10000 Inside Perfect number .
explain : Perfect number is also called complete number or complete number , All its true factors ( I.e. factors other than themselves ) And ( Factor function ) Exactly equal to itself . for example :6( 6 = 1 + 2 + 3 6=1+2+3 6=1+2+3) and 28( 28 = 1 + 2 + 4 + 7 + 14 28=1+2+4+7+14 28=1+2+4+7+14) It's the perfect number . Perfect numbers have many magical properties , If you are interested, you can learn .
Output 100 All prime numbers within .
explain : Prime numbers mean that only 1 A positive integer divided by itself ( barring 1).
Relevant code has been uploaded to https://download.csdn.net/download/weixin_43510203/86242447 If you need help, please check the reference answers by yourself .
边栏推荐
猜你喜欢

生信周刊第37期

【网络空间安全数学基础第9章】有限域

一周精彩内容分享(第13期)

NFT digital collection system construction - app development
![[deserialization vulnerability-02] principle test and magic method summary of PHP deserialization vulnerability](/img/03/f80c82d009d21a938911a155dddf6b.png)
[deserialization vulnerability-02] principle test and magic method summary of PHP deserialization vulnerability

【C和指针第14章】预处理器

How to use a third party without obtaining root permission topic: MIUI chapter

HCIP OSPF接口网络类型实验 第四天

IT圈中的Bug的类型与历史

20000 words detailed explanation, thoroughly understand es!
随机推荐
gcc -l参数和-L参数的区别
[C and pointer Chapter 11] dynamic memory allocation
Svn server and client installation (Chinese package) and simple use
MySQL creates partition tables and automatically partitions them by day
Experience of redis deepwater area -- Interview reference
CCF 1-2 question answering record (1)
The third day of hcip mGRE experiment
JMeter runtime controller
离散分布常用公式及应用场景
MySql的DDL和DML和DQL的基本语法
The art of management - driving software R & D efficiency through leadership
Types and history of bugs in it circle
[markdown grammar advanced] make your blog more exciting (IV: set font style and color comparison table)
Delphi gexperts expert instructions for improving development efficiency
3、 Implementation principle of MFC message mapping mechanism
L1-043 阅览室
[QNX Hypervisor 2.2用户手册]9.2 cmdline
Literature record (part109) -- self representation based unsupervised exemplar selection in a union of subspaces
L1-059 ring stupid bell
Differences between JS map and foreach