当前位置:网站首页>C语言:小乐乐与欧几里得
C语言:小乐乐与欧几里得
2022-07-29 02:17:00 【高邮吴少】
题目链接
小乐乐最近在课上学习了如何求两个正整数的最大公约数与最小公倍数,但是他竟然不会求两个正整数的最大公约数与最小公倍数之和,请你帮助他解决这个问题。
#include<stdio.h>
int main()
{
long long n=0;
long long m=0;
scanf("%d %d",&n,&m);
long long t1=n;
long long t2=m;
while(n%m!=0)
{
long long tmp=n;
n=m;
m=tmp%m;
}
//走完上面的while得到的m就是最大公约数
printf("%lld",m+t1*t2/m);
//这里打印要以lld的形式打印,不然以整形打印还是会溢出
}

我们这里用辗转相除法求得最大公约数
最小公倍数=原m*原n/最大公约数,这个证明在我的另一篇文章里,
已知最大公约数快速求最小公倍数(附证明)
需要注意的是,这里数的范围是[1,2^20],进行求最小公倍数时,因为原m*原n所以会非常大,你用int肯定会溢出,为了防止溢出,我们这里索性直接用long long即可。
边栏推荐
- Polygon zkevm - Introduction to HERMEZ 2.0
- JMeter's BeanShell generates MD5 encrypted data and writes it to the database
- HTTP breakpoint resume and cache problems
- C语言:小乐乐与进制转换
- Polygon zkEVM——Hermez 2.0简介
- really time ntp服务启动命令
- 云开发打工人必备上班摸鱼划水微信小程序源码
- UE4.25 Slate源码解读
- ASEMI整流桥S25VB100,S25VB100参数,S25VB100应用
- Cuda-npp image and video processing
猜你喜欢

ECCV 2022 | AirDet:无需微调的小样本目标检测方法

Mqtt routine

架构师进阶,微服务设计与治理的 16 条常用原则

This blogger has a comprehensive classification of QT. If you are free, go to study and summarize it and record it.

Ten methods to prevent blackmail software from attacking data

Source code and display of 18 classic programs in C language vs2019

Multimodal unsupervised image to image translation

QT屏幕自适应自动布局,拖动窗口自动变大变小(一)

新版海螺影视主题模板M3.1全解密版本多功能苹果CMSv10后台自适应主题开源全解密版

Polygon point test
随机推荐
Redis队列实现秒杀
ECCV 2022 | AirDet:无需微调的小样本目标检测方法
云开发打工人必备上班摸鱼划水微信小程序源码
Rust list (VEC) replication
0728~ sorting out interview questions
Youxuan software appoints Huang Zhijun as the general manager of the company
qt QStringList用法
h. 264 code stream explanation
redis为什么快,消息队列,单线程
In depth analysis - Pretreatment
Wechat applet - Advanced chapter Lin UI component library source code analysis button component (II)
HTTP断点续传以及缓存问题
Servlet三种实现方式
6-21 vulnerability exploitation MySQL weak password cracking
PHP幸运抽奖系统带后台源码
Quickly master nodejs installation and getting started
IOT components
多重继承与派生类成员标识
Introduction to network foundation
Mqtt routine