導航:首頁 > 編程語言 > java兩個數的最大公約束和最大

java兩個數的最大公約束和最大

發布時間:2023-06-12 03:49:23

⑴ 用java求兩個整數的最大公約數、最小公倍數

package com.fmzrt;

/**
* 求兩個數的最大公約數和最小公倍數
* @author kele
*
*/
public class GongyueGongbeiShu {
/**
* 求兩個數的最大公約數
* @param m
* @param n
* @return
*/
public static int MaxGys(int m, int n) {
int r;
while(n != 0) {
r = m % n;
m = n;
n = r;
}
return m;
}
/**
* 求兩個數的最小公倍數
* @param m
* @param n
* @return
*/
public static int MinGbs(int m, int n) {
return m * n / MaxGys(m, n);
}
public static void main(String[] args) {
System.out.println("最大公約數 : (36, 12) = " + GongyueGongbeiShu.MaxGys(36, 12));
System.out.println("最小公倍數 : (36, 12) = " + GongyueGongbeiShu.MinGbs(36,12));
}
}

⑵ 用java求兩數的最大公約數和最小公倍數。

import java.util.*;
public class lianxi06 {
public static void main(String[] args) {
int a ,b,m;
Scanner s = new Scanner(System.in);
System.out.print( "鍵入一個整數: ");
a = s.nextInt();
System.out.print( "再鍵入一個整數: ");
b = s.nextInt();
deff cd = new deff();
m = cd.deff(a,b);
int n = a * b / m;
System.out.println("最大公約數: " + m);
System.out.println("最小公倍數: " + n);
}
}
class deff{
public int deff(int x, int y) {
int t;
if(x < y) {
t = x;
x = y;
y = t;
}
while(y != 0) {
if(x == y) return x;
else {
int k = x % y;
x = y;
y = k;
}
}
return x;
}
}

閱讀全文

與java兩個數的最大公約束和最大相關的資料

熱點內容
js給php變數賦值 瀏覽:446
雜志版本號是什麼意思 瀏覽:223
地圖特效代碼 瀏覽:192
去除思科配置文件中的號 瀏覽:196
運行的16位程序太多 瀏覽:1
蘋果mac用什麼軟體好學編程 瀏覽:681
ai中線段怎麼添加寬度配置文件 瀏覽:956
lol文件怎麼找不到game 瀏覽:142
aecc視頻教程 瀏覽:983
linux怎麼查看資料庫用戶名 瀏覽:182
cefs文件系統 瀏覽:404
學平面設計個編程哪個好 瀏覽:701
如何把編程文件轉為hex 瀏覽:80
清除蘋果地圖來自地址 瀏覽:233
已經打開的文件如何清理 瀏覽:685
視頻網站有什麼用 瀏覽:70
多個表格文件怎樣壓縮文件 瀏覽:729
cad文件大很卡如何解決 瀏覽:633
將java程序打包成apk 瀏覽:277
2021唱吧文件找不到了 瀏覽:463

友情鏈接