当前位置:网站首页>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)
上面给出大家两种求最大公约数和最小公倍数的方法,大家可以根据自己的情况选取不同的方法,鼓励大家两种方法都掌握哦!
上面的程序给出了比较详细的注释,以便新手小白参考。程序的思路设计或者代码实现并不是最优的,欢迎各位大佬指正错误或者给出更优质的思路。
我是一只想成为鲲鹏的菜鸟,大家的鼓励是我前进的动力,欢迎大家点赞收藏评论哦!
边栏推荐
- MySQL锁总结(全面简洁 + 图文详解)
- Implementation of count (*) in MySQL
- .Xmind文件如何上传金山文档共享在线编辑?
- 3.猜数字游戏
- Reinforcement learning series (I): basic principles and concepts
- Floating point comparison, CMP, tabulation ideas
- QT meta object qmetaobject indexofslot and other functions to obtain class methods attention
- Principles, advantages and disadvantages of two persistence mechanisms RDB and AOF of redis
- The difference between cookies and sessions
- 1.初识C语言(1)
猜你喜欢
Write a program to simulate the traffic lights in real life.
Nuxtjs快速上手(Nuxt2)
2022 Teddy cup data mining challenge question C idea and post game summary
SRC挖掘思路及方法
1. Preliminary exercises of C language (1)
【手撕代码】单例模式及生产者/消费者模式
(ultra detailed onenet TCP protocol access) arduino+esp8266-01s access to the Internet of things platform, upload real-time data collection /tcp transparent transmission (and how to obtain and write L
编写程序,模拟现实生活中的交通信号灯。
1.C语言矩阵加减法
Change vs theme and set background picture
随机推荐
仿牛客技术博客项目常见问题及解答(三)
2.初识C语言(2)
Miscellaneous talk on May 27
Comparison between FileInputStream and bufferedinputstream
Differences among fianl, finally, and finalize
5月14日杂谈
This time, thoroughly understand the MySQL index
为什么要使用Redis
关于双亲委派机制和类加载的过程
[the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis
Aurora system model of learning database
8.C语言——位操作符与位移操作符
2022 Teddy cup data mining challenge question C idea and post game summary
7-6 矩阵的局部极小值(PTA程序设计)
这次,彻底搞清楚MySQL索引
1.初识C语言(1)
Zatan 0516
4. Branch statements and loop statements
(ultra detailed onenet TCP protocol access) arduino+esp8266-01s access to the Internet of things platform, upload real-time data collection /tcp transparent transmission (and how to obtain and write L
fianl、finally、finalize三者的区别