Classic example: Daffodil Count/Hundred dollars hundred Chickens/Craps
Practice question: Fibonacci sequence/perfect number/prime number
After studying the previous chapters, I think it is necessary to take you to do some exercises here to consolidate the knowledge you have learned before. Although what we have learned so far is only the tip of the iceberg of Python, these contents are enough for us to build programs. logic in . For beginners of programming languages, after learning the core language elements of Python (variables, types, operators, expressions, branch structures, loop structures, etc.), one thing that must be done is to try to use the knowledge learned Solving real-world problems, in other words, is to exercise one's ability to translate algorithms (methods and steps for solving problems) described in human natural language into Python codes, and this can only be achieved through a lot of practice.
We have sorted out some classic cases and exercises for you in this chapter. We hope that through these examples, on the one hand, we can help you consolidate the knowledge of Python you have learned before, and on the other hand, you can understand how to establish the logic in the program and how to use some simple algorithms. Solve real problems.
Look for daffodil numbers .
Explanation : Narcissus number is also known as super-complete number invariant number, narcissistic number, self-power number, Armstrong number, it is a 3-digit number, and the sum of the cubes of the numbers on each digit of the number is exactly equal to itself, for example : $1^3 + 5^3+ 3^3=153$.
""" 找出所有水仙花数 Version: 0.1 Author: 骆昊 """ 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 above code, we have found the ones, tens and hundreds of three-digit numbers through integer division and modulo operations. This small trick is still commonly used in actual development. Using a similar method, we can also reverse a positive integer, for example: change 12345 into 54321, the code is as follows.
""" 正整数的反转 Version: 0.1 Author: 骆昊 """ num = int(input('num = ')) reversed_num = 0 while num > 0: reversed_num = reversed_num * 10 + num % 10 num //= 10 print(reversed_num)
A hundred dollars and a hundred chickens problem.
Explanation : One hundred coins and one hundred chickens is a mathematical problem proposed by Zhang Qiujian , an ancient mathematician in China , in the book "Suan Jing": one chicken is worth five, one hen is worth three, and three chickens are worth one. If you buy a hundred chickens for a hundred dollars, how much are the rooster, hen mother, and chicks? Translated into modern language is: 5 yuan for a rooster, 3 yuan for a hen, and 3 chicks for 1 yuan. If you buy 100 chickens for 100 yuan, how many roosters, hens, and chicks are there?
""" 《百钱百鸡》问题 Version: 0.1 Author: 骆昊 """ 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('公鸡: %d只, 母鸡: %d只, 小鸡: %d只' % (x, y, z))
The method used above is called the exhaustive method , also known as the violent search method . This method enumerates all possible candidates in the alternative solution one by one and checks whether each candidate matches the description of the problem, and finally gets solution to the problem. This method may seem clumsy, but it is usually a feasible or even good choice for very powerful computers, and if the solution to the problem exists, this method will definitely find it.
CRAPS gambling games .
Description : CRAPS, also known as craps, is a very popular table gambling game in Las Vegas, USA. The game uses two dice, and the player gets points by shaking the two dice to play the game. The simple rule is: if the player rolls the dice for the first time and gets 7 or 11 points, the player wins; if the player rolls 2 points, 3 points or 12 points for the first time, the dealer wins; players with other points continue to roll the dice, If the player rolls out 7 points, the dealer wins; if the player rolls out the first number of points, the player wins; for other points, the player continues to ask for dice until the winner is determined.
""" Craps赌博游戏 我们设定玩家开始游戏时有1000元的赌注 游戏结束的条件是玩家输光所有的赌注 Version: 0.1 Author: 骆昊 """ from random import randint money = 1000 while money > 0: print('你的总资产为:', money) needs_go_on = False while True: debt = int(input('请下注: ')) if 0 < debt <= money: break first = randint(1, 6) + randint(1, 6) print('玩家摇出了%d点' % first) if first == 7 or first == 11: print('玩家胜!') money += debt elif first == 2 or first == 3 or first == 12: print('庄家胜!') money -= debt else: needs_go_on = True while needs_go_on: needs_go_on = False current = randint(1, 6) + randint(1, 6) print('玩家摇出了%d点' % current) if current == 7: print('庄家胜') money -= debt elif current == first: print('玩家胜') money += debt else: needs_go_on = True print('你破产了, 游戏结束!')
### Useful Exercises
- Generate the first 20 numbers of the Fibonacci sequence .Explanation : Fibonacci sequence, also known as the golden section sequence, is an Italian mathematician Leonardo Fibonacci (Leonardoda Fibonacci) in "The Book of Calculation" proposed a rabbit under ideal assumptions The sequence introduced by the problem of growth rate, so this sequence is also jokingly called "rabbit sequence". The characteristic of the Fibonacci sequence is that the first two numbers of the sequence are both 1, and starting from the third number, each number is the sum of the previous two numbers, such as: 1, 1, 2, 3, 5 , 8, 13, 21, 34, 55, 89, 144, .... The Fibonacci sequence has direct applications in modern physics, quasi-crystal structure, chemistry and other fields.
- Find the perfect number up to 10000 .Explanation : A perfect number is also called a perfect number or a complete number, and the sum of all its true factors (that is, factors other than itself) (that is, the factor function) is exactly equal to itself. For example: 6 ($6=1+2+3$) and 28 ($28=1+2+4+7+14$) are perfect numbers. Perfect numbers have many magical properties, and those who are interested can understand them by themselves.
- Print all prime numbers up to 100 .Explanation : A prime number refers to a positive integer (excluding 1) that can only be divisible by 1 and itself.
The reference answers to the above exercises are in the code directory corresponding to this chapter. If you need help, please check the reference answers by yourself.