当前位置:网站首页>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 !
边栏推荐
- Differences among fianl, finally, and finalize
- [the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis
- [au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP
- [MySQL database learning]
- 强化学习系列(一):基本原理和概念
- 【数据库 三大范式】一看就懂
- 仿牛客技术博客项目常见问题及解答(三)
- Yugu p1012 spelling +p1019 word Solitaire (string)
- MySQL中count(*)的实现方式
- [graduation season · advanced technology Er] goodbye, my student days
猜你喜欢
3. Input and output functions (printf, scanf, getchar and putchar)
Strengthen basic learning records
Have you encountered ABA problems? Let's talk about the following in detail, how to avoid ABA problems
[面试时]——我如何讲清楚TCP实现可靠传输的机制
MySQL锁总结(全面简洁 + 图文详解)
自定义RPC项目——常见问题及详解(注册中心)
Mode 1 two-way serial communication is adopted between machine a and machine B, and the specific requirements are as follows: (1) the K1 key of machine a can control the ledi of machine B to turn on a
SRC挖掘思路及方法
. How to upload XMIND files to Jinshan document sharing online editing?
1143_ SiCp learning notes_ Tree recursion
随机推荐
TypeScript快速入门
FAQs and answers to the imitation Niuke technology blog project (I)
Redis实现分布式锁原理详解
【Numpy和Pytorch的数据处理】
PriorityQueue (large root heap / small root heap /topk problem)
[the Nine Yang Manual] 2022 Fudan University Applied Statistics real problem + analysis
[experiment index of educator database]
canvas基础1 - 画直线(通俗易懂)
实验九 输入输出流(节选)
自定义RPC项目——常见问题及详解(注册中心)
Implementation of count (*) in MySQL
Programme de jeu de cartes - confrontation homme - machine
Custom RPC project - frequently asked questions and explanations (Registration Center)
【头歌educoder数据表中数据的插入、修改和删除】
ArrayList的自动扩容机制实现原理
【数据库 三大范式】一看就懂
[modern Chinese history] Chapter V test
Attach the simplified sample database to the SQLSERVER database instance
Package bedding of components
[modern Chinese history] Chapter 6 test