Python100Days study notes --- Day5 construction program logic

After completing a few chapters earlier, I think it is necessary here to take you to do some exercises to consolidate the knowledge learned before, although so far we learn the contents of Python's just tip of the iceberg, but these elements have been enough for us to build a program in logic. For beginners programming language, in after learning the Python core language elements (variables, types, operators, expressions, branched structure, cyclic structure, etc.), you must do one thing is to try to use what they have learned solve real-world problems, in other words training algorithm (method to solve the problem and steps) translated into Python code their own ability to use human natural language, and this issue must in order to reach through a lot of practice.

We are all in this chapter compiled some classic examples and exercises, I hope these examples, on the one hand knowledge of Python before you help to consolidate what they have learned on the other hand to help you learn how to build a program and how to apply some simple logic algorithms solve real-world problems.

The classic example
to find a few daffodils.

Description: narcissistic number is also known as super variables not fully digital, narcissistic number, since a power of, Armstrong number, which is a 3-digit number, each digit of the digital sum is exactly equal to the cube itself, e.g. : 1 3 + 5 3 + 3 3 = 153 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 divisible by modulo operation, respectively, and had a bit to find a three-digit, ten and one hundred such tips in the actual development is still commonly used. In a similar manner, we can also achieve a reverse positive integer, for example: 54321 to 12345 into, as shown in the code below.

"""
正整数的反转

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)

One hundred money one hundred chicken problem.

Description: one hundred money one hundred chickens ancient Chinese mathematician Zhang Qiu Jian in mathematical problem "was considered" a book made of: a chicken Weng worth five chickens mother a valuable three chicks a valuable three. One hundred to buy one hundred chicken, chicken and asked Weng, mother chicken, chicks each geometry? Modern culture is translated into: 5 yuan a rooster, a hen 3 yuan, one yuan three chicks, one hundred dollars to buy 100 chickens, ask rooster, hens, chicks have how many?

"""
《百钱百鸡》问题

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 above method is called a brute-force method, also called the violence a search method, this method through all the possible candidates listed one by one alternative solution and check the description of each candidate meets the problem, finally get solution of the problem. This approach looks awkward, but computing power for very powerful computers, it is usually a viable even is a good choice, but if there is solution of the problem, this method will be able to find it.

CRAPS casino games.
Description: CRAPS also known as Craps, Las Vegas is a very popular one table gambling game. The game uses two dice, game players earn points by shaking two dice. The rules are simple: Players roll the dice if the first roll out of the 7:00 or 11:00, the player wins; if the players shake out first 2:00, 3:00 or 12:00, Zhuang Jiasheng; other points the players continue to roll the dice, If the player shake out the 7:00, Zhuang Jiasheng; if the players shake out the first shake of points, the player wins; other points, players continue to die until a winner.

"""
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('你破产了, 游戏结束!')
Published 124 original articles · won praise 141 · views 160 000 +

Guess you like

Origin blog.csdn.net/weixin_36838630/article/details/105205879