Blue Bridge Cup Python Breakthrough for Primary School Students | Matchstick Equation

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

Attach a summary post: Pupils Blue Bridge Cup Python Breakthrough | Summary_Blue Bridge Cup Python Elementary School Group_COCOgsta's Blog-CSDN Blog



【Description】

Given n matches, how many equations of the form A+B=C can you spell out? A, B, and C in the equation are integers spelled out with matchsticks (if the number is non-zero, the highest bit cannot be 0). The spelling of numbers 0-9 with matchsticks is shown in the figure:

Notice:

  1. The plus sign and the equal sign each require two matchsticks;
  2. If A≠B, then A+B=C and B+A=C are regarded as different equations (A, B, C≥0);
  3. All n matchsticks must be used.

【Enter description】

An integer n (1≤n≤24).

【Output description】

An integer, the number of different equations that can be spelled.

【Sample input】

14

【Sample output】

2

【Code Explanation】

n = int(input())

res = 0
arr = [0 for i in range(1001)]
nums = [6,2,5,5,4,5,6,3,7,6]
[nums.append(0) for i in range(1001)]


def dfs(x, sum):
    global res
    if sum > n: return

    if x > 3:
        if arr[1] + arr[2] == arr[3] and sum == n:
            # for i in range(1, 4):
            #     print(arr[i], end =' ')
            # print()
            res += 1
        return

    for i in range(0, 1001):
        arr[x] = i
        dfs(x+1, sum + nums[i])
        arr[x] = 0


n -= 4
for i in range(10, 1001):
    nums[i] = nums[i%10] + nums[i//10]
dfs(1, 0)
print(res)

【operation result】

18
9

 

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/130722764
Recommended