導航:首頁 > 編程語言 > java公因數

java公因數

發布時間:2023-09-07 08:42:25

A. java最大公約數演算法

三種演算法:
//歐幾里得演算法(輾轉相除):
public static int gcd(int m,int n) {
if(m<n) {
int k=m;
m=n;
n=k;
}
//if(m%n!=0) {
// m=m%n;
// return gcd(m,n);
//}
//return n;
return m%n == 0?n:gcd(n,m%n);
}

//連續整數檢測演算法:
public static int gcd1(int m,int n) {
int t;
if(m<n) {
t=m;
}else {
t=n;
}
while(m%t!=0||n%t!=0){
t--;
}
return t;
}

//公因數法:(更相減損)
public static int gcd2(int m,int n) {
int i=0,t,x;
while(m%2==0&n%2==0) {
m/=2;
n/=2;
i++;
}
if(m<n){
t=m;
m=n;
n=t;
}
while(n!=(m-n)) {
x=m-n;
m=(n>x)?n:x;
n=(n<x)?n:x;
}
if(i==0)
return n;
else
return (int)Math.pow(2, i)*n;
}
public static void main(String[] args) {
System.out.println("請輸入兩個正整數:");
Scanner scan = new Scanner(System.in);
Scanner scan2=new Scanner(System.in);
int m=scan.nextInt();
int n=scan2.nextInt();
System.out.println("歐幾里得演算法求最大公約數是:"+gcd(m,n));
System.out.println("連續整數檢測演算法求最大公約數是:"+gcd1(m,n));
System.out.println("公因數法求最大公約數是:"+gcd2(m,n));
}
}

B. 編寫java Application輸入兩個數並找出兩個數的最大公因數

import java.util.Scanner;

public class Du2 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("請輸入第一個數字 ");
int num1 = scanner.nextInt();

System.out.print("請輸入第2個數字 ");
int num2 = scanner.nextInt();

int temp = num1;

num1 = num1 < num2? num1: num2;
num2 = temp > num2? temp: num2;

for(int num = num1; num >= 1; num--){
if(num1 % num == 0 && num2 % num == 0){
System.out.println("最大公約數為 : " + num);
break;
}
}

}

}

C. 如何用JAVA求兩個整數的最大公因數

public class Test {
public static void main(String[] args){
Test t = new Test();
int x = 240;
int y = 880;
System.out.println(t.gcd(x,y));
}
private int gcd(int x,int y){
int re = 0;
while(x!=y){
if(x>y){
x=x-y;
re =x;
gcd(x,y);
}
else{
y=y-x;
re = y;
gcd(x,y);
}
}
return re;
}
}
運行結果:
80

D. Java編程題:計算數字12和18的最小公倍數。求答案。

下面給出了一個計算兩個整數的最大公約數和最小公倍數的通用的方法:
首先先計算最大的公約數,最小公倍數=兩個數的乘積再除以它們的最大公約數。

public class Test2 {
public static void main(String[] args) {
System.out.println(lcm(12, 18));
}

/**
* 計算整數a和b的最大公約數
*/
public static int gcd(int a, int b) {
while(b!=0) {
int tmp = b;
b = a%b;
a = tmp;
}
return a;
}
/**
* 計算整數a和b的最小公倍數
*/
public static int lcm(int a, int b) {
int t = gcd(a,b);
if(t==0) return 0;
else
return a * b / t;
}
}

E. java 最大公因數,最小公倍數,因式分解如何寫這些方法

package neusoft.com.test;

public class Test2 {
public static void main(String args[]){
int x = 18,y = 39;
int result1 = getMax(x,y);
System.out.println(result1);
int result2 = getMin(x,y);
System.out.println(result2);
}
//最大公約數
public static int getMax(int x,int y){
int tmp = 0;
if(x > y){
tmp = y;
}else{
tmp = x;
}
for(int i = tmp; i >= 1; i--){
if(x % i == 0 && y % i == 0){
return i;
}
}

return -1;
}
//最小公倍數
public static int getMin(int x,int y){
int tmp = 0;
if(x < y){
tmp = y;
}else{
tmp = x;
}
for(int i = tmp; i > 0; i++){
if(i % x == 0 && i % y == 0){
return i;
}
}

return -1;
}
}

閱讀全文

與java公因數相關的資料

熱點內容
方舟手游如何修改伺服器後台 瀏覽:815
程序員考試教程 瀏覽:219
文件名修改為文件夾的名字批處理 瀏覽:251
拍照程序員 瀏覽:827
wps怎麼把pdf轉jpg 瀏覽:217
自拍用什麼app做的藝術照 瀏覽:169
h3c無線配置命令 瀏覽:515
linux代碼閱讀工具 瀏覽:160
能夠畫出對稱圖形的是什麼app 瀏覽:424
單片機投票器 瀏覽:467
程序員那麼可愛唱嗎 瀏覽:830
手機誤刪的app怎麼恢復 瀏覽:700
java第三方加密庫 瀏覽:660
編譯代碼軟體哪個好 瀏覽:997
編譯器軟體圖片 瀏覽:880
美團專送app怎麼不接受遠單 瀏覽:833
伺服器mgmt口如何連接電腦 瀏覽:798
做程序員至少要精通幾種 瀏覽:673
個人用雲伺服器價格對比 瀏覽:257
如何遠程刪除伺服器文件夾 瀏覽:779