最大公约数和最小公倍数
2013-03-16 15:02
246 查看
题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
在循环中,只要除数不等于0,用较大数除以较小的数,
将小的一个数作为下一轮循环的大数,
取得的余数作为下一轮循环的较小的数,
如此循环直到较小的数的值为0,返回较大的数,此数即为最大公约数,
最小公倍数为两数之积除以最大公约数
在循环中,只要除数不等于0,用较大数除以较小的数,
将小的一个数作为下一轮循环的大数,
取得的余数作为下一轮循环的较小的数,
如此循环直到较小的数的值为0,返回较大的数,此数即为最大公约数,
最小公倍数为两数之积除以最大公约数
import java.util.*; public class Test{ //求最大公约数 public int cal(int a,int b){ //使得a>=b if(a<b){ int f = a; a=b; b=f; } while(b!=0){ if(a==b) return a; else{ int k = a%b; a=b; b=k; } } return a; } //求的两个数的最大公约数 public int cal2(int a,int b){ return a*b/cal(a,b); } public static void main(String args[]){ int a,b,m,n; Scanner s = new Scanner(System.in); System.out.println("please input a integer: "); a = s.nextInt(); System.out.println("pleanse input another integer: "); b = s.nextInt(); m = new Test().cal(a,b); n = new Test().cal2(a,b); System.out.println("result is: "+m); System.out.println("result2 is: "+n); } }
相关文章推荐
- 取两个数最大公约数,最小公倍数
- GCD -- 最大公约数 LCM -- 最小公倍数
- c++实验7-最大公约数和最小公倍数
- C++实验7—最大公约数和最小公倍数
- C++实验7-最大公约和和最小公倍数
- 最大公约数与最小公倍数
- (数论)最大公约数和最小公倍数问题
- 最大公约数和最小公倍数
- 最小公倍数与最大公约数
- 最大公约数和最小公倍数
- 辗转相除法求最大公约数,附带求最小公倍数
- c语言:输入两个正整数m和n,求其最大公约数和最小公倍数
- 8.最大公约数和最小公倍数问题
- 最大公约数和最小公倍数
- 求最小公倍数和最大公约数的方法
- 如何求两个数的最大公约数,最小公倍数
- 最大公约数与最小公倍数
- 最大公约数与最小公倍数
- C实现最大公约数和最小公倍数
- 7828:最大公约数与最小公倍数