Primary school students Blue Bridge Cup Python breakthrough |

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】

Monkey loves prime numbers very much. He borrowed n (n≤10) cards from his friend Xiaomei, and each card has a number ci (1-1000). The little monkey chooses a number of cards (at least 1), and if the sum of these numbers is a prime number, he will immediately do 10 push-ups, 10 sit-ups, 10 squats... He wants to know , how many ways to choose the middle card can make him enter the state of chicken blood? (As long as the cards are different, even if the numbers are the same, they are considered different selection methods)

Please use the program to help him calculate.

【Enter description】

Enter a total of 2 lines:

Line 1, 1 positive integer n.

In line 2, n positive integers c1, c2, c3, ..., cn are separated by commas.

【Output description】

A positive integer representing the total number of options

【Sample input】

4

7,11,13,17

【Sample output】

7

【Code Explanation】

n = int(input())
c = [int(i) for i in input().split(",")]
use = [False for i in range(n)]
cnt = 0

def isPrime(x):
    if x <= 1:
        return False
    i = 2
    while i * i <= x:
        if x % i == 0:
            return False
        i += 1
    return True

def dfs(step):
    global n,c,use,cnt
    if step>=n:
        to = 0
        for i in range(n):
            if use[i] == True:
                to += c[i]
        if isPrime(to):
            cnt += 1
        return
    use[step] = False
    dfs(step + 1)
    use[step] = True
    dfs(step + 1)

dfs(0)
print(cnt)
复制代码

【operation result】

4
7,11,13,17
7

Guess you like

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