educoder: experimental six multiple cycles

Level 1: Display the complete number within the input number

 mission details

The task of this level: display the complete number within the input number. A number is said to be a perfect number if it is equal to the sum of all its factors (excluding the number itself). For example 6=1+2+3, so 6 is a perfect number.

programming requirements

According to the prompt, supplement the code in the editor on the right to display the complete number within the input number.

Test case user input 1000 output 6 28 496


Turning words into actions is much more difficult than turning actions into words. Let's start your mission, I wish you success!

code:

x=eval(input())
for i in range (1,x):
    y = 0
    for j in range(1, i):
        if i%j == 0:
          y += j;
    if y == i:
        print(i)

Level 2: Output the factorial equation

mission details

The task of this level: input x output from 1! to x! Equation such as input 10 output

Let's start your mission, I wish you success!

code:

x=eval(input())
for i in range(1,x+1):
    y ,k= 1,""
    for j in range(1,i+1):
        y *=j
        if j != 1:
            k = k+'*'+str(j)   
        else:
            k = k+str(j) 
    print("{}!={}={}".format(i,k,y))

Level 3: Output numbers that meet the requirements

mission details

The task of this level: Input a number x (between 1-9), output all the numbers in each digit not exceeding x, and the numbers of each digit are different from each other. Each line displays 10 numbers Note: each There is a space prompt after the number: you can set end=" " in print, and a space will be displayed after the output expression. For example, enter 5, and the output is as shown below

Let's start your mission, I wish you success!

code:

x=eval(input())
list = [0,1,2,3,4,5,6,7,8,9]
a,count= list[0:(x+1)],0
for i in range(1,x+1):
    for j in range(0,x+1):
        if a[j] != a[i]:
            s = str(a[i])+str(a[j])
        else:
            continue
        for k in range(0,x+1):
            if a[k] != a[i] and a[k] != a[j]:
                m ,count= s+str(a[k])+' ',count+1
                print(m,end='' if count%10 != 0 else "\n" )
            else:
                continue

Level 4: Chickens and rabbits in the same cage

mission details

The task of this level: the chicken and the rabbit are locked in a cage, input the number of heads and the number of feet, how many rabbits and chickens are locked in the cage? If there is no number that meets the condition, it will display no solution

Test instruction

The platform will test the code you write:

Test input: 36 100 Expected output: 22 chickens, 14 rabbits

Test input: 20 40 Expected output: No solution


If you don't want to waste your life in the world, you have to study for a lifetime. Let's start your mission, I wish you success!

code:

a=eval(input())
b=eval(input())
if 2*a>=b:
    print("无解")
else:
    for x in range(1, a):
        y = a - x
        if 2 * x + 4 * y == b:
            print("鸡" + str(x) + "只兔子" + str(y) + "只")

Level 5: The shortest wood remaining

mission details

The task of this level: Find the cutting scheme of the shortest remaining wood. Enter the length of a piece of material, which needs to be cut into short pieces with a length of 19 meters and 23 meters. How many pieces of the two short materials are cut, and the remaining material is the least? (Each short material must be greater than or equal to one piece)

Test instruction

The platform will test the code you write:

Test input: 100 Expected output: 4 segments at 19 meters, 1 segment at 23 meters, and 1 meter remaining

Test input: 133 Expected output: 19 meters 2 sections, 23 meters 4 sections, remaining 3 meters Note: The length of the wood must be greater than or equal to 42


Let's start your mission, I wish you success!

code:

s=eval(input())
if s>=42:
    x1,x2= (s-42)//23,(s-42)%23
    if x2+x1*4<19:
        n1 = 1,
        n2 = x1+1
        zx = x2
    elif x2>=19:
        n1 = 2
        n2 = x1+1
        zx = x2-19
    else:
        for i in range(1,x1+1):
            if x2+4*i>=19:
                n1 = i+2
                n2 = x1-i+1
                zx = (x2+4*i)%19
                break
else:
    print("木材不符合")
print("19米{}段,23米{}段,剩余{}米".format(n1,n2,zx))

Level 6: Print graphics

mission details

The task of this level: Write a small program that can calculate and print graphics. Input uppercase letters and print a related graphic. For example, input E to print the graphic as follows

related information

In order to complete the task of this level, you need to master: 1. ord and chr functions, 2. How to control line break.

chr and ord functions

The ord function can convert the character into the code value you need, and the chr function can convert the code value into the character you need. Through such conversion, you can conveniently complete the conversion operation between characters and code values, ord('A') is 65 chr(65) is 'A' chr(ord('A')+1) corresponds to the character 'B'

control line break

By default, the print statement in python will be displayed in a new line. print (printed content, end="") will not be displayed in a new line.

Test instruction

The platform will test the code you write:

Test Input: C; Expected Output: A ABA ABCBA

Test input: G; Expected output:

Let's start your mission, I wish you success!

code:

b=input("")
b=ord(b)-64
for i in range(int(b)):
    for j in range(1,b-i):
        print(" ",end="")
    for k in range(i):
        print(chr(65+k),end="")
    for l in range(i+1):
        print(chr(65+i-l),end="")
    print()

Level 7: Logical Inference

mission details

The task of this level: When a judge tried a theft case, he interrogated the four suspects A, B, C, and D involved. The four confessed as follows: A: "The criminal is among B, C, and D." B: "I did not commit the crime, but C stole it." C: "One of A and D is a criminal." D : "B is telling the truth" After full investigation, it was confirmed that only two of the four told the truth, and there was only one criminal. Please output the encoding of the real criminal For example, if A is a criminal, output A is a criminal

Tip: You can use the variable a to be 0 to indicate that a is not a criminal, and a to be 1 to indicate that a is a criminal, and then loop through the two possibilities for each person


Let's start your mission, I wish you success!

code:

for a in (0,1):
    for b in (0,1):
        for c in (0,1):
            for d in (0,1):
                x=(b==1 or c==1 or d==1)
                y=(b==0 and c==1)
                z=(a==1 or d==1)
                if((x and z and not y)or(y and not x and not z)) and a+b+c+d==1:
                    if a==1:
                        print("A是罪犯")
                    elif b==1:
                        print("B是罪犯")
                    elif c==1:
                        print("C是罪犯")
                    else:
                        print("D是罪犯")

Level 8: Titles of Multi-person Tang Poems

mission details

The task of this level: In the Tang Poetry.txt file, as shown in the figure below. The title of each poem is the serial number poet colon poem title.

Write a program, input the poet's name, display the title of the poem written by the poet, and the number of works, as shown in the figure.

If the poet has no works, show No Works. It is required to be able to cycle through the poet's name for searching until an empty string is entered.

Tip: After querying a poet, you need to move the file pointer to the beginning of the file through f1.seek(0)

Those who dare to learn will never be idle.

Let's start your mission, I wish you success!

code:

f1=open("sy6//唐诗.txt","r",encoding="utf-8")
while True:
    x,count=input("诗人姓名"),0
    f1.seek(0)
    if x=="":
        break
    for line in f1.readlines():
        line=line.strip('\n')
        if(line.find(x)!=-1):
            count=count+1
            print(line)
    if count!=0:
        print("一共{}首".format(count))
    else:
        print("无作品")
f1.close()

Guess you like

Origin blog.csdn.net/weixin_62174595/article/details/127076423