The 10th Blue Bridge Cup java-c group-rectangular cutting

1. Problem description:

Total score for this question: 5 points
Xiao Ming has some rectangular materials, and he wants to cut out some squares from these rectangular materials. When he faced a piece of rectangular material, he always cut from the middle, cut out the largest square, and the remaining rectangle, and then cut the remaining rectangular material until all cut into squares. For example, for a piece of material with 5 and 3 on both sides (denoted as 5×3), Xiao Ming will cut out 4 squares in turn, 3×3, 2×2, 1×1, and 1×1. Now Xiao Ming has a rectangular piece of material with two sides of 2019 and 324 respectively. How many squares will Xiao Ming cut out in the end?

2. Thinking analysis:

Analyzing the problem, we can know that each time the side length with the smaller length is cut as the side length of the square, so that the side length of the square obtained by cutting is the largest, and it will stop until it can not be cut at the end. According to this repetitive operation, then we write it as A loop can be used to simulate the entire process. Anyway, repetitive actions can be written in the loop for simulation . At the beginning, you can verify whether the rectangle with a length and width of 5-3 is cut correctly, and then verify that the length and width are 2019. The number of squares obtained by cutting a rectangle of -324. Since the data for this question is not particularly large, manual calculations are also possible

3. The code is as follows:

if __name__ == '__main__':
    # 长度为2019-324的矩形能够切成多少个正方形
    # 感觉使用一个循环即可: 每一次切割长度最大的直到最后x, y成为了最小的正方形1-1, 因为每一次都是切割最大的正方形
    # 手计也可以
    x, y = 2019, 324
    res = 0
    f = 1
    while f:
        if x == 0 or y == 0:
            f = 0
            break
        if y > x: x, y = y, x
        t = x
        x = y
        y = t - y
        res += 1
        # 输出剩下来的矩形的长宽
        # print("长度与宽度为: ", x, y)
    print(res)

 

Guess you like

Origin blog.csdn.net/qq_39445165/article/details/114997656