Blue Bridge Cup Daily One Question (12): Guess the age (Xiao Ming) (python)

Topic:

It's simple.
Xiao Ming took his two younger sisters to the Lantern Festival. When someone asked them how old they were, they said mischievously: "The product of our age is 6 times the sum of our ages."
Xiao Ming added: "They are not twins, and the difference in age must be no more than 8 years old."
Please write: the age of Xiao Ming's younger sister.

Solution:

Fill-in-the-blank questions are mainly fast.
Set the younger sister’s age to i and keep increasing the value of i.
While increasing the value of i, set the older sister’s age to j.
When j is less than i + 8 and satisfies the product of i and j, i and j When the sum of j is 6 times, the
smaller i is output

Code:

for i in range(1, 50):
    j = i + 1
    
    while j <= i + 8:
        if j * i == (j + i) * 6:
            print(i)
            break
        else:
            j += 1

Answer:
10

Guess you like

Origin blog.csdn.net/weixin_50791900/article/details/112793956