Java学习笔记10:求两个数的最小公倍数和最大公约数

/**
 * @author:朱凌风
 * @weather:多云转晴
 * @date:06/18/2011
 * @function:求两个数的最小公倍数和最大公约数
 */

package com.jerome;

public class MaxAndMin {

 public static void main(String[] args) {
  GreatestCommonDivisor gcd=new GreatestCommonDivisor(26,4);
  System.out.println("最大公约数是:"+gcd.calculate());
  LeastCommonMultiple lcm=new LeastCommonMultiple(26,4);
  System.out.println("最小公倍数是是:"+lcm.calculate());
 }
}

//最大公约数类
class GreatestCommonDivisor {
 int m=0;
 int n=0;
 
 GreatestCommonDivisor(int m,int n) {
  this.m=m;
  this.n=n;
 }
 
 //计算最大公约数
 public int calculate(int m,int n) {
  int temp=0;
  if(m<n) {
   //交换m与n
   temp=n;
   n=m;
   m=temp; 
  }
  if(n==0) {
   return m;
  }else {
   return calculate(n,m%n);
  }
 }
 
 public int calculate() {
  return this.calculate(this.m,this.n);
 }
}
 
//最小公倍数类
class LeastCommonMultiple {
 int m=0;
 int n=0;
 
 LeastCommonMultiple(int m,int n) {
  this.m=m;
  this.n=n;
 }
 
 //计算最小公倍数
 public int calculate() {
  GreatestCommonDivisor gcd=new GreatestCommonDivisor(this.m,this.n);
  int min=this.m*this.n/gcd.calculate();
  return min;
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值