当前位置:网站首页>7-15 h0161. Find the greatest common divisor and the least common multiple (PTA program design)

7-15 h0161. Find the greatest common divisor and the least common multiple (PTA program design)

2022-07-06 13:56:00 Programming Lindaiyu

Enter two positive integers a and b, Find the greatest common divisor and the least common multiple .

Input format :

Enter... On one line 2 No more than one. 100000 The positive integer A and B.

Output format :

stay 2 Output in line A、B The greatest common divisor and the least common multiple of .

sample input :

42 36

sample output :

The greatest common divisor is :6
The least common multiple is :252

Code (Python): 

Method 1 ( Easier to understand ):

a,b=map(int,input().split())  # Input of two numbers 
d=max(a,b)  # use Python The built-in function finds the larger of the two numbers 
x=min(a,b)  ## use Python The built-in function finds the smaller of the two numbers 
# Find the greatest common divisor 
for i in range(x,0,-1):  # Because the greatest common divisor must be smaller than both numbers , And find the maximum value in the common divisor , So start with a smaller number and try until 0( barring 0), Because it's trying backwards , So the step size is set to -1
    if a%i==0 and b%i==0:  # If you encounter a number that can be divided by two numbers at the same time , Then it must be the greatest common divisor , be break Exit loop 
        print(" The greatest common divisor is :%d"%i)  # Output... In format 
        break  # Exit loop 
 # Find the least common multiple 
for i in range(d,a*b+1):  # Because the least common multiple must be greater than both numbers , And find the minimum value of the common multiple , So try from the larger number to a*b, because a*b It must be their common multiple , And the least common multiple must be less than or equal to a*b
    if i%a==0 and i%b==0:  # If you encounter a number that can divide two numbers at the same time , Then it must be the least common multiple 
        print(" The least common multiple is :%d"%i)  # Output... In format 
        break  # Exit loop 

Method 2 :

a,b=map(int,input().split())   # Note that this can't be a=int(input())   b=int(input()) because input Will read all strings in a row 
m=a  # The first a,b The value of , In case the following will a,b After changing the value of a,b The real value is lost 
n=b
res=m%n   # To find the greatest common divisor, first divide the two numbers and take the remainder 
while(res!=0):   # Determine whether the remainder is 0, if 0 Then the divisor is the greatest common divisor 
    a=b          # If not 0, Then assign the value of the divisor to the dividend , Assign the remainder to the divisor 
    b=res
    res=a%b      # Continue to divide , Ready for the next cycle 
x1=b  # Used to store the value of the final divisor , The greatest common divisor 
x2=m*n/x1  # According to a mathematical formula , That is, the least common multiple is the product of two numbers and then divided by the greatest common divisor 
print(" The greatest common divisor is :%d"%x1)  # Output... In format 
print(" The least common multiple is :%d"%x2)

Here are two ways to find the greatest common divisor and the least common multiple , You can choose different methods according to your own situation , Encourage everyone to master both methods !

The above program gives more detailed comments , For novice Xiaobai's reference . The idea of program design or code implementation is not optimal , You are welcome to correct your mistakes or give better ideas .

I am a rookie who wants to be Kunpeng , Everyone's encouragement is my driving force , Welcome to like collection comments !

原网站

版权声明
本文为[Programming Lindaiyu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060917047296.html