当前位置:网站首页>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即可。
边栏推荐
- 0728~面试题梳理
- Only when you are far away will you miss
- In depth analysis - Pretreatment
- Memories of many years ago
- 6 years of testing experience, teaching you how to test ~ how to control the project
- 网络基础概论
- Family relationship calculator wechat applet source code
- Ten methods to prevent blackmail software from attacking data
- ECCV 2022 | airdet: a small sample target detection method without fine tuning
- ASEMI整流桥S25VB100,S25VB100参数,S25VB100应用
猜你喜欢

Ten methods to prevent blackmail software from attacking data

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

6-21 vulnerability exploitation MySQL weak password cracking

Redis主从模式、哨兵集群、分片集群

第五天实验

Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译

Introduction to network foundation

Multimodal unsupervised image to image translation

Flink生产环境经典问题汇总

6 years of testing experience, teaching you how to test ~ how to control the project
随机推荐
Split, an avalanche caused by connection pool parameters
Understanding service governance in distributed development
云开发打工人必备上班摸鱼划水微信小程序源码
白马过隙的时光
主从复制及其原理
Flink生产环境经典问题汇总
自动分账系统哪家好?
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
架构师进阶,微服务设计与治理的 16 条常用原则
Read the recent trends of okaleido tiger and tap the value and potential behind it
Master-slave replication and its principle
C语言实现三子棋游戏
owt-server源码剖析(四)--video模块分析之Mixer Out
Wechat applet - Advanced chapter Lin UI component library source code analysis button component (II)
STM32C8T6编码器电机测速与arduino光电模块测速
nacos名字的由来
Kbxxxxx is not necessarily a patch, but also a description of a solution to a problem
Rust list (VEC) replication
九宫格心形拼图小程序源码/带流量主微信小程序源码
Intel's IPP Library (Integrated Performance Primitives)