当前位置:网站首页>如何求最大公约数?
如何求最大公约数?
2022-08-09 12:25:00 【进击的李知因】
1、在1~m/2之间找约数,最后的约数即最大公约数
public class Case25_GreatestCommonDivisor{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入两个正整数:");
int m = in.nextInt();
int n = in.nextInt();
int gcd = 1;
if(m>n) //交换,确保m为较小者
{
m = m^n;
n = m^n;
m = m^n;
}
if(m == n || n%m == 0)
{
System.out.printf("%d与%d的最大公约数是%d\n",m,n,m);
System.exit(0);
}
//找约数,最后的即最大公约数
for(int i = 1; i <= m/2; i++)
if(m%i==0 && n%i==0)
gcd = i;
System.out.printf("%d与%d的最大公约数是%d\n",m,n,gcd);
}
}
2、在m/2~1之间找约数,找到就退出循环
for(int i = m/2; i > 0; i--)
if(m%i==0 && n%i==0)
{
gcd = i;
break;
}
3、欧几里德算法(辗转相除法)
public class Case25_GreatestCommonDivisor{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("请输入两个正整数:");
int m = in.nextInt();
int n = in.nextInt();
if(m>n) //交换,确保m为较小者
{
m = m^n;
n = m^n;
m = m^n;
}
while(n%m != 0)
{
int temp = m;
m = n%m;
n = temp;
}
System.out.printf("%d与%d的最大公约数是%d\n",m,n,m);
}
}
边栏推荐
- AI basketball referee, walking is special, ask harden care don't care
- 超越CLIP的多模态模型,只需不到1%的训练数据!南加大最新研究来了
- Go Affair, How to Become a Gopher and Find a Go Job in 7 Days, Part 1
- 金融业“限薪令”出台/ 软银出售过半阿里持仓/ DeepMind新实验室成立... 今日更多新鲜事在此...
- MySQL 原理与优化,Group By 优化 技巧
- WebView注入Js代码实现大图自适应屏幕点击图片预览详情
- Use RecyclerView to implement three-level collapsed list
- 二叉树的序列化和反序列化
- FFmpeg库在win10上配置使用(不配置libx264)
- The FFmpeg library is configured and used on win10 (libx264 is not configured)
猜你喜欢

1-hour live broadcast recruitment order: industry big names share dry goods, and enterprise registration opens丨qubit·viewpoint

26. Pipeline parameter substitution command xargs

已解决IndentationError: unindent does not match any oute r indentation Level

保存Simulink仿真模型为图片或者PDF的方法

MySQL 原理与优化,Group By 优化 技巧

中断系统结构及中断控制详解

内网穿透工具ngrok使用教程

报告:想学AI的学生数量已涨200%,老师都不够用了

ansible-cmdb friendly display ansible collects host information

World's 4th mad scientist dies on his 103rd birthday
随机推荐
Do you know the difference between comments, keywords, and identifiers?
Flutter入门进阶之旅(五)Image Widget
ABP中的数据过滤器 (转载非原创)
Intranet penetration tool ngrok usage tutorial
#Internet of Things essay#Xiaoxiong pie equipment development actual combat
00后写个暑假作业,被监控成这笔样
告别手摇织布机的AI时代
8、IDEA提交代码出现: Fetch failed fatal: Could not read from remote repository
FFmpeg compiles and installs on win10 (configure libx264)
WebView注入Js代码实现大图自适应屏幕点击图片预览详情
中科院打脸谷歌:普通电脑追上量子优越性,几小时搞定原本要一万年的计算...
Win10 compiles the x264 library (there are also generated lib files)
关于Retrofit网络请求URL中含有可变参数的处理
注:检测到当前使用的ADB不是HBuilder内置或自定义ADB:PID为:9544进程名称为:adb.exe 路径为:c:\users\administrator\appdata\local\and
ABAP interview questions: how to use the System CALL interface of the ABAP programming language, direct execution ABAP server operating System's shell command?
【微服务~远程调用】整合RestTemplate、WebClient、Feign
Flutter Getting Started and Advanced Tour (8) Button Widget
超越CLIP的多模态模型,只需不到1%的训练数据!南加大最新研究来了
AI篮球裁判火了,走步算得特别准,就问哈登慌不慌
ViewPager fragments of nested data blank page abnormal problem analysis