最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Java求两个正整数的最大公约数和最小公倍数
时间:2022-06-29 01:39:17 编辑:袖梨 来源:一聚教程网
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
程序分析:利用辗除法。
最大公约数:
| 代码如下 | 复制代码 |
publicclassCommonDivisor{ publicstaticvoidmain(String args[]) { commonDivisor(24,32); } staticintcommonDivisor(intM,intN) { if(N<0||M<0) { System.out.println("ERROR!"); return-1; } if(N==0) { System.out.println("the biggest common divisor is :"+M); returnM; } returncommonDivisor(N,M%N); } } | |
最小公倍数和最大公约数:
| 代码如下 | 复制代码 |
importjava.util.Scanner; publicclassCandC { //下面的方法是求出最大公约数 publicstaticintgcd(intm,intn) { while(true) { if((m = m % n) ==0) returnn; if((n = n % m) ==0) returnm; } } publicstaticvoidmain(String args[])throwsException { //取得输入值 //Scanner chin = new Scanner(System.in); //int a = chin.nextInt(), b = chin.nextInt(); inta=23;intb=32; intc = gcd(a, b); System.out.println("最小公倍数:"+ a * b / c +"n最大公约数:"+ c); } } | |
相关文章
- gmail邮箱登录-网页版gmail官网登录入口 01-05
- 夸克网页版入口-夸克浏览器最新版登录入口 01-05
- 蛙漫2台版在线入口-蛙漫2最新访问地址 01-05
- 知乎网页版入口免费直达-知乎在线问答平台一键登录 01-05
- 国家中小学网络云平台官网-免费网课登录入口 01-05
- 搜狗浏览器极速网页版入口-搜狗搜索2026全新网页版登录地址 01-05