Blue Bridge Cup Python Breakthrough for Primary School Students | Roast Chicken

Learn Python from a baby! Record the questions in the Blue Bridge Cup Python learning and test preparation process, and record every moment.

Attached is a summary post: Pupils Blue Bridge Cup Python Breakthrough | Summary_COCOgsta's Blog-CSDN Blog


【Description】

Hanke the pig likes to eat roast chicken very much (this is the same animal, why is it too urgent to cook!) Hanke eats chicken very special, why is it so special? Because he has 10 ingredients (mustard, cumin, etc.), each ingredient can put 1 to 3 grams, and the deliciousness of any roast chicken is the sum of the quality of all ingredients.

Now, Hanke wants to know, if you are given a delicious level n, please output all the matching schemes of these 10 ingredients.

【Enter description】

A positive integer n, indicating the degree of deliciousness.

【Output description】

The first line, the total number of solutions.

From the second line to the end, there are 10 numbers, indicating the quality of each ingredient, arranged in lexicographical order.

If there is no method that meets the requirements, just output a 0 on the first line.

【Sample input】

11

【Sample output】

10

1 1 1 1 1 1 1 1 1 2

1 1 1 1 1 1 1 1 2 1

1 1 1 1 1 1 1 2 1 1

1 1 1 1 1 1 2 1 1 1

1 1 1 1 1 2 1 1 1 1

1 1 1 1 2 1 1 1 1 1

1 1 1 2 1 1 1 1 1 1

1 1 2 1 1 1 1 1 1 1

1 2 1 1 1 1 1 1 1 1

2 1 1 1 1 1 1 1 1 1

【Code Explanation】

n = int(input())

arr = [0 for i in range(10+1)] #存临时方案
mem = [[0 for i in range(10+1)] for i in range(100+1)] #存所有方案
res = 0

def dfs(x, sum):
    global res
    if sum > n: return
    if x > 10:
        if (sum == n):
            res += 1
            for i in range(1, 10+1):
                mem[res][i] = arr[i]
        return

    for i in range(1, 3+1):
        arr[x] = i
        dfs(x+1, sum+i)
        arr[x] = 0 #恢复现场


dfs(1, 0)
# 打印方案数
print(res)
# 打印具体方案
for i in range(1, res+1):
    for j in range(1, 10+1):
        print(mem[i][j], end=" ")
    print()
复制代码

【operation result】

11
10
1 1 1 1 1 1 1 1 1 2 
1 1 1 1 1 1 1 1 2 1 
1 1 1 1 1 1 1 2 1 1 
1 1 1 1 1 1 2 1 1 1 
1 1 1 1 1 2 1 1 1 1 
1 1 1 1 2 1 1 1 1 1 
1 1 1 2 1 1 1 1 1 1 
1 1 2 1 1 1 1 1 1 1 
1 2 1 1 1 1 1 1 1 1 
2 1 1 1 1 1 1 1 1 1 

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/130481545