当前位置:网站首页>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)上面给出大家两种求最大公约数和最小公倍数的方法,大家可以根据自己的情况选取不同的方法,鼓励大家两种方法都掌握哦!
上面的程序给出了比较详细的注释,以便新手小白参考。程序的思路设计或者代码实现并不是最优的,欢迎各位大佬指正错误或者给出更优质的思路。
我是一只想成为鲲鹏的菜鸟,大家的鼓励是我前进的动力,欢迎大家点赞收藏评论哦!
边栏推荐
- 【九阳神功】2022复旦大学应用统计真题+解析
- 稻 城 亚 丁
- Safe driving skills on ice and snow roads
- Aurora system model of learning database
- Implementation principle of automatic capacity expansion mechanism of ArrayList
- Caching mechanism of leveldb
- [the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis
- 【手撕代码】单例模式及生产者/消费者模式
- MATLAB打开.m文件乱码解决办法
- Detailed explanation of redis' distributed lock principle
猜你喜欢

5.函数递归练习

Cookie和Session的区别

8. C language - bit operator and displacement operator

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

4. Binary search
![[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP](/img/d6/109042b77de2f3cfbf866b24e89a45.png)
[au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP

MySQL锁总结(全面简洁 + 图文详解)

编写程序,模拟现实生活中的交通信号灯。

C语言入门指南

2.C语言初阶练习题(2)
随机推荐
杂谈0516
(原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
Custom RPC project - frequently asked questions and explanations (Registration Center)
7. Relationship between array, pointer and array
2022泰迪杯数据挖掘挑战赛C题思路及赛后总结
ArrayList的自动扩容机制实现原理
QT meta object qmetaobject indexofslot and other functions to obtain class methods attention
Write a program to simulate the traffic lights in real life.
Comparison between FileInputStream and bufferedinputstream
简单理解ES6的Promise
5月27日杂谈
9. Pointer (upper)
[the Nine Yang Manual] 2019 Fudan University Applied Statistics real problem + analysis
【九阳神功】2019复旦大学应用统计真题+解析
6. Function recursion
(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
[the Nine Yang Manual] 2018 Fudan University Applied Statistics real problem + analysis
3. Input and output functions (printf, scanf, getchar and putchar)
为什么要使用Redis
4.分支语句和循环语句