輸入兩個正整數(shù)m和n, 求其最大公約數(shù)和最小公倍數(shù).

站在用戶的角度思考問題,與客戶深入溝通,找到柳江網(wǎng)站設(shè)計與柳江網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:做網(wǎng)站、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、域名注冊、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋柳江地區(qū)。
用輾轉(zhuǎn)相除法求最大公約數(shù)
算法描述:
m對n求余為a, 若a不等于0
則 m - n, n - a, 繼續(xù)求余
否則 n 為最大公約數(shù)
最小公倍數(shù) = 兩個數(shù)的積 / 最大公約數(shù)
#include
int main()
{
int m, n;
int m_cup, n_cup, res; /*被除數(shù), 除數(shù), 余數(shù)*/
printf("Enter two integer:\n");
scanf("%d %d", m, n);
if (m 0 n 0)
{
m_cup = m;
n_cup = n;
res = m_cup % n_cup;
while (res != 0)
{
m_cup = n_cup;
n_cup = res;
res = m_cup % n_cup;
}
printf("Greatest common divisor: %d\n", n_cup);
printf("Lease common multiple : %d\n", m * n / n_cup);
}
else printf("Error!\n");
return 0;
}
★ 關(guān)于輾轉(zhuǎn)相除法, 搜了一下, 在我國古代的《九章算術(shù)》中就有記載,現(xiàn)摘錄如下:
約分術(shù)曰:“可半者半之,不可半者,副置分母、子之數(shù),以少減多,更相減損,求其等也。以等數(shù)約之。”
其中所說的“等數(shù)”,就是最大公約數(shù)。求“等數(shù)”的辦法是“更相減損”法,實際上就是輾轉(zhuǎn)相除法。
輾轉(zhuǎn)相除法求最大公約數(shù),是一種比較好的方法,比較快。
對于52317和75569兩個數(shù),你能迅速地求出它們的最大公約數(shù)嗎?一般來說你會找一找公共的使因子,這題可麻煩了,不好找,質(zhì)因子大。
現(xiàn)在教你用輾轉(zhuǎn)相除法來求最大公約數(shù)。
先用較大的75569除以52317,得商1,余數(shù)23252,再以52317除以23252,得商2,余數(shù)是5813,再用23252做被除數(shù),5813做除數(shù),正好除盡得商數(shù)4。這樣5813就是75569和52317的最大公約數(shù)。你要是用分解使因數(shù)的辦法,肯定找不到。
那么,這輾轉(zhuǎn)相除法為什么能得到最大公約數(shù)呢?下面我就給大伙談?wù)劇?/p>
比如說有要求a、b兩個整數(shù)的最大公約數(shù),a>b,那么我們先用a除以b,得到商8,余數(shù)r1:a÷b=q1…r1我們當然也可以把上面這個式子改寫成乘法式:a=bq1+r1------l)
如果r1=0,那么b就是a、b的最大公約數(shù)3。要是r1≠0,就繼續(xù)除,用b除以r1,我們也可以有和上面一樣的式子:
b=r1q2+r2-------2)
如果余數(shù)r2=0,那么r1就是所求的最大公約數(shù)3。為什么呢?因為如果2)式變成了b=r1q2,那么b1r1的公約數(shù)就一定是a1b的公約數(shù)。這是因為一個數(shù)能同時除盡b和r1,那么由l)式,就一定能整除a,從而也是a1b的公約數(shù)。
反過來,如果一個數(shù)d,能同時整除a1b,那么由1)式,也一定能整除r1,從而也有d是b1r1的公約數(shù)。
這樣,a和b的公約數(shù)與b和r1的公約數(shù)完全一樣,那么這兩對的最大公約數(shù)也一定相同。那b1r1的最大公約數(shù),在r1=0時,不就是r1嗎?所以a和b的最大公約數(shù)也是r1了。
有人會說,那r2不等于0怎么辦?那當然是繼續(xù)往下做,用r1除以r2,……直到余數(shù)為零為止。
在這種方法里,先做除數(shù)的,后一步就成了被除數(shù),這就是輾轉(zhuǎn)相除法名字的來歷吧。
public?class?Gcd?{
public?static?void?main(String[]?args)?{
for(int?i=0;i10;i++)?{
int?a=(int)(Math.random()*99+1);
int?b=(int)(Math.random()*99+1);
System.out.println(a+","+b+"\t=\t"+getNumber(a,b));
}
}
public?static?int?getNumber(int?m,int?n){
if?(m?%?n?==?0)?{
return?n;
}
else?{
return?getNumber(n,m?%?n);
}
}
}
最大公約數(shù)求的沒問題。
求最小公倍數(shù)的時候,return (a*b)/m;這句代碼中的a和b的值已經(jīng)在父類代碼中被
do {
temp_number = a%b;
a=b;
b=temp_number;
}
修改了,所以最終a*b就是0了。
改正:
class Son extends Father{
int m;
int x;
int y;
Son(int a,int b)
{
x=a;
y=b;
super(a,b);
}
public int f(){
m=super.f();
return (x*y)/m;
}
}
這樣應(yīng)該就行了
自然語言描述計算兩個非負整數(shù)p 和q 的最大公約數(shù):若q 是0,則最大公約數(shù)為p。否則,將p 除以q 得到余數(shù)r,p 和q 的最大公約數(shù)即為q 和r 的最大公約數(shù)。Java code 求公約數(shù)
public static int gcd(int p, int q){ if (q == 0) return p; int r = p % q; return gcd(q, r);}
public static int g(int p, int q){ return p*q/gcd(q, r);}
這個你用遞歸的方法啊。
while(a%b!=0)
{
a=Math.max(a%b,b);
b=Math.min(a%b,b);
}
這個一段代碼不對啊。而且顯然a%b比b小嘛。何必要max。min呢?你直接賦值肯定不對。要跟上面一樣。來個中間值大小換一下。
遞歸方法如下:
public
static
void
main(String[]
args)
{
int
a,b,answer;
Scanner
in=new
Scanner(System.in);
a=in.nextInt();
b=in.nextInt();
if(ab)
{
answer=test(a,b);
}else{
answer=test(b,a);
}
System.out.println("最大公約數(shù)是:"+answer);
}
private
static
int
test(int
a,
int
b)
{
//
TODO
Auto-generated
method
stub
if(a%b==0){
return
b;
}else{
return
test(b,a%b);
}
}
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( "鍵入一個整數(shù): ");
a = s.nextInt();
System.out.print( "再鍵入一個整數(shù): ");
b = s.nextInt();
deff cd = new deff();
m = cd.deff(a,b);
int n = a * b / m;
System.out.println("最大公約數(shù): " + m);
System.out.println("最小公倍數(shù): " + 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;
}
}
新聞名稱:最大公約數(shù)java代碼,JAVA求最大公約數(shù)
文章來源:http://chinadenli.net/article5/dsiohii.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、品牌網(wǎng)站建設(shè)、網(wǎng)站排名、域名注冊、ChatGPT、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)