PTA 7-12 Violent Elementary School (Grade 2)-Find 2 three-digit numbers

 

Xiao Bo is a second-grade student in Xincheng Primary School. This kid is very honest, and his math and Chinese scores are also at the middle level. However, once sitting in front of the computer and writing code, it is a bit domineering, especially good at various violent methods. This question is one of Mathematics teacher Mo's questions, please solve it violently with Xiaobo.

Input format:

The input is the difference D (the difference D in the example in the figure is 93), and D is a positive integer.

Output format:

The first line outputs the number K of all solutions, and then the K line outputs all solutions in ascending order of the minuend.

Input sample:

For example enter:

93

Sample output:

output:

4
246-153
354-261
516-423
624-531

 Code

D = int(input())  # 输入差值
lis = [1,2,3,4,5,6]  
lis1 = [0]*6  # 用来临时存放解
number = 0   # 计算解的个数
lis2 = []  # 放置最终符合要求的解
# 暴力循环所有组合求解
for i in range(0,len(lis)):
    lis1[0] = lis[i]     # 放入第一个数
    for k in range(0,len(lis)):
        if lis[k] != lis1[0]:  # 放入第二个数,不能与前一个相同
            lis1[1] = lis[k]
        else :# 如果相同,则依次选取后一个数
            continue    
        for j in range(0,len(lis)):
            if lis[j] != lis1[0] and lis[j] != lis1[1]:   # 放入第三个数,不能与前面的相同,后面依次类推
                lis1[2] = lis[j]
            else :
                continue
            for h in range(0,len(lis)):
                if lis[h] != lis1[0] and lis[h] != lis1[1] and lis[h] != lis1[2]:
                    lis1[3] = lis[h]
                else :
                    continue
                for v in range(0,len(lis)):
                    if lis[v] != lis1[0] and lis[v] != lis1[1] and lis[v] != lis1[2] and lis[v] != lis1[3] :
                        lis1[4] = lis[v]
                    else :
                        continue
                    for x in range(0,len(lis)):
                        if lis[x] != lis1[0] and lis[x] != lis1[1] and lis[x] != lis1[2] and lis[x] != lis1[3] and lis[x] != lis1[4] :
                            lis1[5] = lis[x]
                        else :
                            continue
                        # 判断得到的解如果符合要求,则放入lis2,后面输出
                        if ((lis1[0]*100+lis1[1]*10+lis1[2])-(lis1[3]*100+lis1[4]*10+lis1[5])) == D:
                            number += 1 
                            lis2.append(lis1[0]*100+lis1[1]*10+lis1[2])
                            lis2.append(lis1[3]*100+lis1[4]*10+lis1[5])
                            
print(number)
for p in range(0,len(lis2),2):
    print('{}-{}'.format(lis2[p],lis2[p+1]))

 Submit results

 

Guess you like

Origin blog.csdn.net/weixin_58707437/article/details/127945835