当前位置:网站首页>7-15 h0161. 求最大公约数和最小公倍数(PTA程序设计)
7-15 h0161. 求最大公约数和最小公倍数(PTA程序设计)
2022-07-06 09:22:00 【编程林黛玉】
输入两个正整数a和b,求其最大公约数和最小公倍数。
输入格式:
输入在一行中给出2个不超过100000的正整数A和B。
输出格式:
在2行中输出A、B的最大公约数和最小公倍数。
输入样例:
42 36
输出样例:
最大公约数为:6
最小公倍数为:252
代码(Python):
方法一(比较容易理解):
a,b=map(int,input().split()) #两个数的输入
d=max(a,b) #用Python内置函数找出两个数中较大的
x=min(a,b) ##用Python内置函数找出两个数中较小的
#求最大公约数
for i in range(x,0,-1): #因为最大公约数一定比两个数都小,且求的是公约数中的最大值,所以从较小的数开始一直尝试到0(不包括0),因为是倒着往前试,所以步长置为-1
if a%i==0 and b%i==0: #如果遇到了可以被两个数同时整除的数,那它一定是最大公约数,则break退出循环
print("最大公约数为:%d"%i) #按格式输出
break #退出循环
#求最小公倍数
for i in range(d,a*b+1): #因为最小公倍数一定比两个数都大,且求的是公倍数中的最小值,所以从较大的那个数一直开始尝试到a*b,因为a*b一定是他俩的公倍数,且最小公倍数一定小于等于a*b
if i%a==0 and i%b==0: #如果遇到可以同时整除两个数的,那它一定是最小公倍数
print("最小公倍数为:%d"%i) #按格式输出
break #退出循环方法二:
a,b=map(int,input().split()) #注意这里不能是a=int(input()) b=int(input())因为input会读取一行中的所有字符串
m=a #先把a,b的值记录下来,以防下面的将a,b的值改变后使a,b真正的值丢失
n=b
res=m%n #求最大公约数先将两数相除取余
while(res!=0): #判断余数是否为0,若为0则除数就是最大公约数
a=b #若不为0,则将除数的值赋给被除数,将余数的赋给除数
b=res
res=a%b #继续相除,准备进入下一次循环
x1=b #用来存放最终除数的值,即最大公约数
x2=m*n/x1 #根据一个数学公式,即最小公倍数等于两数乘积再除以最大公约数
print("最大公约数为:%d"%x1) #按格式输出
print("最小公倍数为:%d"%x2)上面给出大家两种求最大公约数和最小公倍数的方法,大家可以根据自己的情况选取不同的方法,鼓励大家两种方法都掌握哦!
上面的程序给出了比较详细的注释,以便新手小白参考。程序的思路设计或者代码实现并不是最优的,欢迎各位大佬指正错误或者给出更优质的思路。
我是一只想成为鲲鹏的菜鸟,大家的鼓励是我前进的动力,欢迎大家点赞收藏评论哦!
边栏推荐
- Service ability of Hongmeng harmonyos learning notes to realize cross end communication
- [the Nine Yang Manual] 2019 Fudan University Applied Statistics real problem + analysis
- PriorityQueue (large root heap / small root heap /topk problem)
- 5月14日杂谈
- [graduation season · advanced technology Er] goodbye, my student days
- Cookie和Session的区别
- 【九阳神功】2019复旦大学应用统计真题+解析
- ArrayList的自动扩容机制实现原理
- 稻 城 亚 丁
- 【毕业季·进击的技术er】再见了,我的学生时代
猜你喜欢

1.初识C语言(1)

2022 Teddy cup data mining challenge question C idea and post game summary

2. Preliminary exercises of C language (2)

QT meta object qmetaobject indexofslot and other functions to obtain class methods attention

FAQs and answers to the imitation Niuke technology blog project (II)

Questions and answers of "signal and system" in the first semester of the 22nd academic year of Xi'an University of Electronic Science and technology

5. Download and use of MSDN

C language Getting Started Guide

仿牛客技术博客项目常见问题及解答(三)

MySQL lock summary (comprehensive and concise + graphic explanation)
随机推荐
Differences among fianl, finally, and finalize
抽象类和接口的区别
20220211-CTF-MISC-006-pure_ Color (use of stegsolve tool) -007 Aesop_ Secret (AES decryption)
3. Number guessing game
[the Nine Yang Manual] 2017 Fudan University Applied Statistics real problem + analysis
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
简述xhr -xhr的基本使用
js判断对象是否是数组的几种方式
扑克牌游戏程序——人机对抗
【九阳神功】2020复旦大学应用统计真题+解析
透彻理解LRU算法——详解力扣146题及Redis中LRU缓存淘汰
C语言入门指南
【九阳神功】2018复旦大学应用统计真题+解析
优先队列PriorityQueue (大根堆/小根堆/TopK问题)
C语言入门指南
A comprehensive summary of MySQL transactions and implementation principles, and no longer have to worry about interviews
ArrayList的自动扩容机制实现原理
C language to achieve mine sweeping game (full version)
The difference between overloading and rewriting
vector