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

熱點內容
dede工具 瀏覽:507
5g網盟app怎麼下載 瀏覽:486
微信備份老是連接中斷 瀏覽:886
出台多少份文件 瀏覽:380
鞋子怎麼搭配衣服的app 瀏覽:755
文件名使用的通配符的符號是什麼 瀏覽:916
lol分卷文件損壞怎麼辦 瀏覽:276
6分管車螺紋怎麼編程 瀏覽:732
海口農商銀行信用卡app是什麼 瀏覽:770
win10任務欄文件夾我的電腦 瀏覽:14
安卓nba2k18 瀏覽:776
文件夾密碼怎麼修改密碼 瀏覽:271
蘋果數據中心用什麼伺服器 瀏覽:769
省內圓通快遞寄文件夾需要多少錢 瀏覽:740
iphone程序加密 瀏覽:884
win10文件夾調整文件行高 瀏覽:681
創意手繪教程 瀏覽:754
微信刪除帳號信息 瀏覽:596
mysql操作類文件 瀏覽:649
繞過xp密碼 瀏覽:158

友情鏈接