1000 Python programming cases (8): Special recruitment, admission and selection, thief catching, Armstrong number cases

This series of articles provides readers with more detailed exercise questions through 1000 examples (one article represents 1 instance), so that readers can learn from other things and learn deeply. The articles in this series involve Python knowledge points including: Python language basics, operators and expressions, statements and program structures, lists and tuples, dictionaries and sets, strings, regular expressions, functions, object-oriented programming, modules, and Package, exception handling and program debugging, file and directory operations, database programming, interface programming, network programming, WEB programming, processes and threads, web crawlers, game programming and other knowledge points, from easy to difficult, from shallow to deep, step by step A solid programming foundation.

The algorithms involved in this series of articles include search, backtracking, recursion, sorting, iteration, greedy, divide and conquer, and dynamic programming. The data structures involved include strings, lists, pointers, intervals, queues, matrices, stacks, linked lists, hashes Table, line segment tree, binary tree, binary search tree, graph structure, etc.

This series of articles is the author, in order to adapt to the innovation requirements of the current education reform, better practice language courses, and meet the needs of practical teaching and innovative ability training. Read a lot of books, interview algorithms of major Internet companies, LintCode, LeetCode, and nine Chapter Algorithms and a series of articles written by the author based on the author’s project experience in recent years, selected 1000 interesting and practical application examples, from the aspects of different difficulty, different algorithms, different types and different data structures, the actual algorithm is carried out. To sum up, I hope to provide inspiration for Python programmers. Due to the author's limited experience and level, omissions and improprieties in the blog post are unavoidable. I sincerely hope that readers can provide more valuable comments and specific revision suggestions in the comment area for the author to further modify and improve.

1. Case: Special Recruitment, Admission and Selection

Requirements: Assuming that a school recruits special students, the following three admission standards are set.
The first type, if the piano level is 9 or above, and the computer level is 4 or above, pass it directly.
The second type, if the culture class is very good, you can appropriately lower the specialty standard, the piano level is 5 or above, and the computer level is 2 or above.
In the third type, if the culture course is passed, it will be admitted according to normal standards, that is, the piano level is 7 or above, and the computer level is 3 or above. According to the above set conditions, write a simple special recruitment admission detection program, the demonstration effect is shown in the following figure: the


complete code of the case is as follows:

# -*- coding: UTF-8 -*-
"""
@author:AmoXiang
@file:20.特招录取选拔.py
@time:2021/01/22
"""

while True:
    stu_id = int(input("请输入考号:").strip())
    whk = float(input("文化课成绩: ").strip())
    piano = int(input("钢琴等级: ").strip())
    computer = int(input("计算机等级: ").strip())
    if 20180100 < stu_id < 20181000:
        if (piano >= 9 and computer >= 4) or \
                (whk >= 90 and piano >= 5 and computer >= 2) \
                or (whk >= 60 and piano >= 7 and computer >= 3):
            print("恭喜,您被我校录取!")
        else:
            print("很遗憾,您未被我校录取!")
    else:
        print("考号输入有误,请重新输入!")
    print("=================下一位:===================")

2. Case: Catch a thief

Requirements: The police arrested four criminal suspects a, b, c, and d. One of them was a thief. The interrogation statement is as follows.
a said: "I am not a thief."
b said: "c is a thief."
c said: "the thief must be d."
d said: "c nonsense."
In the above statement, three people are known to be telling the truth. A person is telling lies, please write a program to infer who is the thief. The demonstration effect is shown in the figure below: the

Insert picture description here
complete code of the sample is shown below:

# -*- coding: UTF-8 -*-
"""
@author:AmoXiang
@file:18.抓小偷.py
@time:2021/01/22
"""

for i in range(1, 5):
    if 3 == ((i != 1) + (i == 3) + (i == 4) + (i != 4)):
        thief = chr(96 + i)  # 将1、2、3、4转换为a、b、c、d
        print(f"{thief} 是小偷")

Denote a, b, c, and d as 1, 2, 3, and 4 respectively, and loop through each suspect. Assuming that the loop variable i is a thief, use the variable i to bring in the expression, and judge each suspect's confession separately to determine whether it is true, and there can only be three true.

3. Case: Armstrong Number

Requirements: "Armstrong number" means that if an n-digit positive integer is equal to the sum of the n-th power of its digits, then the number is called an Armstrong number. Among them, when n is 3, it is a special "Armstrong number", called "daffodil number". For example, 1634 is an "Armstrong number" because 1634=1**4+6**4+3**4+4**4. Please enter a number and write a program to determine whether the number is an Armstrong number. The demonstration effect is as shown in the figure below: the
Insert picture description here
complete code of the case is as follows:

# -*- coding: UTF-8 -*-
"""
@author:AmoXiang
@file:19.阿姆斯特朗数.py
@time:2021/01/22
"""

while True:
    # 输入一个整数 赋值给两个变量,一个变量用于最后的判断,一个用于数据操作
    num1 = num2 = int(input("请输入一个数:").strip())  # 1634
    length = len(str(num2))  # 获取该数的长度:4
    get_sum = 0  # 定义一个整数
    for i in range(length):  # range(4)
        if num2 > 0:
            # 第一次:4**4
            # 第二次:3**4
            # 第三次: 6**4
            # 第四次: 1**4 ==> 16*16+9*9+36*36+1==>256+81+1296+1==>1634
            get_sum += (num2 % 10) ** length
        num2 //= 10

    if num1 == get_sum:  # 判断原来数num1和求和后的数get_sum是否相等
        print(f"{num1} 是阿姆斯特朗数")
    else:
        print(f"{num1} 不是阿姆斯特朗数")

Thank you for reading this blog post, and hope this article can be your leader in programming. I wish you a happy reading!


Insert picture description here

    A good book never gets tired of reading a hundred times. And if I want to be the most beautiful boy in the audience, I must insist on learning to acquire more knowledge, use knowledge to change my destiny, use blog to witness growth, and use actions to prove that I am working hard.
    If my blog help you, if you like my blog, please 点赞, 评论,收藏 a key triple Oh! I heard that those who like it will not be too bad luck, and will be full of vitality every day! If you really want to be a prostitute, I wish you happy every day, and welcome to my blog.
 Coding is not easy, and your support is my motivation to stick to it. After the thumbs do not forget 关注me!

Guess you like

Origin blog.csdn.net/xw1680/article/details/113001052