導航:首頁 > 編程語言 > 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兩個數的最大公約束和最大相關的資料

熱點內容
cdr導出圖片不顯示文件名 瀏覽:761
pcdmis如何離線編程 瀏覽:201
微信推文插入文件 瀏覽:844
生產文件櫃圖片 瀏覽:244
如何設置微信數據增加 瀏覽:95
excel文件不再增大 瀏覽:613
ug編程刀路怎麼復制不用滑鼠選取 瀏覽:97
excel文件打開擴展名錯誤 瀏覽:10
阿里巴巴怎麼做數據包 瀏覽:442
無線網路連接一直顯示未連接 瀏覽:25
windows7如何自動激活工具下載 瀏覽:57
天語救磚工具 瀏覽:839
網路體育課程有哪些 瀏覽:19
蘋果微信怎麼查看文件 瀏覽:165
安卓nba2k14面補替換 瀏覽:450
路由器密碼定時換 瀏覽:626
aicc恢復文件在哪裡 瀏覽:167
怎麼查無線網路屬性 瀏覽:874
只狼的聲音文件放在哪裡 瀏覽:219
lol盒子修復在哪個文件夾 瀏覽:308

友情鏈接