亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java求兩個(gè)正整數(shù)的最大公約數(shù)和最小公倍數(shù)

 更新時(shí)間:2017年02月21日 22:24:32   投稿:mdxy-dxy  
這篇文章主要介紹了輸入兩個(gè)正整數(shù)m和n,求其最大公約數(shù)和最小公倍數(shù),需要的朋友可以參考下

題目:輸入兩個(gè)正整數(shù)m和n,求其最大公約數(shù)和最小公倍數(shù)。

程序分析:利用輾除法。

最大公約數(shù):

public class CommonDivisor{
  public static void main(String args[])
  {
    commonDivisor(24,32);
  }
  static int commonDivisor(int M, int N)
  {
    if(N<0||M<0)
    {
      System.out.println("ERROR!");
      return -1;
    }
    if(N==0)
    {
      System.out.println("the biggest common divisor is :"+M);
      return M;
    }
    return commonDivisor(N,M%N);
  }
}

最小公倍數(shù)和最大公約數(shù):

import java.util.Scanner;
public class CandC
{
  //下面的方法是求出最大公約數(shù)
  public static int gcd(int m, int n)
  {
    while (true)
    {
      if ((m = m % n) == 0)
        return n;
      if ((n = n % m) == 0)
        return m;
    }
  }
  public static void main(String args[]) throws Exception
  {
    //取得輸入值
    //Scanner chin = new Scanner(System.in);
    //int a = chin.nextInt(), b = chin.nextInt();
    int a=23; int b=32;
    int c = gcd(a, b);
    System.out.println("最小公倍數(shù):" + a * b / c + "\n最大公約數(shù):" + c);
  }
}

大家可以參考腳本之家以前發(fā)布的文章。

相關(guān)文章

  • java顯示當(dāng)前運(yùn)行時(shí)的參數(shù)(java運(yùn)行參數(shù))

    java顯示當(dāng)前運(yùn)行時(shí)的參數(shù)(java運(yùn)行參數(shù))

    這篇文章主要介紹了java顯示當(dāng)前運(yùn)行時(shí)參數(shù)的示例(java運(yùn)行參數(shù)),需要的朋友可以參考下
    2014-04-04
  • 最新評論