#includestdio.h
創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于做網(wǎng)站、成都網(wǎng)站制作、山丹網(wǎng)絡(luò)推廣、成都小程序開(kāi)發(fā)、山丹網(wǎng)絡(luò)營(yíng)銷、山丹企業(yè)策劃、山丹品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供山丹建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:muchs.cn
int gcd(int m, int n);
int lcd(int m, int n);
int main()
{
int a, b;
printf("輸入兩個(gè)正整數(shù):");
scanf("%d%d", a, b);
printf("%d 和 %d 最大公約數(shù)為%d\n", a, b, gcd(a, b));
printf("最小公倍數(shù)為:%d\n", lcd(a, b));
}
int gcd(int m, int n)
{
int t;
if (mn)
{
t = m;
m = n;
n = t;
}
while (n != 0)
{
t = m%n;
m = n;
n = t;
}
return m;
}
int lcd(int m, int n)
{
int t;
t = m*n / gcd(m, n);
return t;
}
#includestdio.h
int main()
{
int zdgys(int x, int y); //求最大公約數(shù)
int zxgbs(int x, int y); //求最小公倍數(shù)
int a,b,max,min;
scanf("%d %d",a,b);
max = zdgys(a, b); //求最大公約數(shù)
min = zxgbs(a, b); //求最小公倍數(shù)
printf("最大公約數(shù)為%d,最小公倍數(shù)為%d",max,min);
return 0;
}
int zdgys(int x, int y)
{
int i,r,t=xy ? x : y;
for (i=1;i=t;i++)
{
if (x%i == 0 y%i == 0)
r=i;
}
return r;
}
int zxgbs(int x, int y)
{
int i,t=xy ? x : y;
for (i = t;;)
{
if (i%x == 0 i%y == 0)
break;
else
i++;
}
return i;
}
#include stdio.h
int main()
{
int a,b,c,m,t;
printf("請(qǐng)輸入兩個(gè)數(shù):\n");
scanf("%d%d",a,b);
if(ab)
{
t=a;
a=b;
b=t;
}
m=a*b;
c=a%b;
while(c!=0)
{
a=b;
b=c;
c=a%b;
}
printf("最大公約數(shù)是:\n%d\n",b);
printf("最小公倍數(shù)是:\n%d\n",m/b);
}
擴(kuò)展資料
算法思想
利用格式輸入語(yǔ)句將輸入的兩個(gè)數(shù)分別賦給 a 和 b,然后判斷 a 和 b 的關(guān)系,如果 a 小于 b,則利用中間變量 t 將其互換。
再利用輾轉(zhuǎn)相除法求出最大公約數(shù),進(jìn)而求出最小公倍數(shù)。最后用格式輸出語(yǔ)句將其輸出。
#includestdio.h是在程序編譯之前要處理的內(nèi)容,稱為編譯預(yù)處理命令。編譯預(yù)處理命令還有很多,它們都以“#”開(kāi)頭,并且不用分號(hào)結(jié)尾,所以是c語(yǔ)言的程序語(yǔ)句。
沒(méi)有“最小公約數(shù)”,只有“最小公倍數(shù)”。
最大公約數(shù):
指兩個(gè)或多個(gè)整數(shù)共有約數(shù)中最大的一個(gè)。
最小公倍數(shù):
指兩個(gè)或多個(gè)整數(shù)公有的倍數(shù)中最小的一個(gè),另外,公約數(shù),亦稱“公因數(shù)”。它是一個(gè)能被若干個(gè)整數(shù)同時(shí)均整除的整數(shù)?!?/p>
C語(yǔ)言4種常見(jiàn)算法:
//C語(yǔ)言實(shí)現(xiàn) 四種方法求最大公約數(shù)
// 2019 03
// WANTING WANG
#includestdio.h
#includestdlib.h
#includetime.h
#includemath.h
//輾轉(zhuǎn)相除法
int gcd(int a,int b)
{
if(a%b==0)
return b;
else;
return gcd(b,a%b);
}
//窮舉法
int divisor (int a, int b) //自定義函數(shù)求兩數(shù)的最大公約數(shù)
{
int ?temp;//定義整型變量
temp=(ab)?b:a;//采種條件運(yùn)算表達(dá)式求出兩個(gè)數(shù)中的最小值
while(temp0)
{
if(a%temp==0b%temp==0)//只要找到一個(gè)數(shù)能同時(shí)被a,b所整除,則中止循環(huán)
break;
temp--;//如不滿足if條件則變量自減,直到能被a,b所整除
}
return (temp);//返回滿足條件的數(shù)到主調(diào)函數(shù)處
}
//更相減損法
int gcd2(int m,int n)
{
int i=0,temp,x;
while(m%2==0n%2==0)//判斷m和n能被多少個(gè)2整除
{
m/=2;
n/=2;
i+=1;
}
if(mn)//m保存大的值
{
temp=m;
m=n;
n=temp;
}
while(x)
{
x=m-n;
m=(nx)?n:x;
n=(nx)?n:x;
if(n==(m-n))
break;
}
if(i==0)
return n;
else
return (int) pow(2,i)*n;
}
//Stein算法
int Stein( unsigned int x, unsigned int y )
/* return the greatest common divisor of x and y */
{
int factor = 0;
int temp;
if ( x y )
{
temp = x;
x = y;
y = temp;
}
if ( 0 == y )
{
return 0;
}
while ( x != y )
{
if ( x 0x1 )
{/* when x is odd */
if ( y 0x1 )
{/* when x and y are both odd */
y = ( x - y ) 1;
x -= y;
}
else
{/* when x is odd and y is even */
y = 1;
}
}
else
{/* when x is even */
if ( y 0x1 )
{/* when x is even and y is odd */
x = 1;
if ( x y )
{
temp = x;
x = y;
y = temp;
}
}
else
{/* when x and y are both even */
x = 1;
y = 1;
++factor;
}
}
}
return ( x factor );
}
int main()
{
int i;
int a[30];
for(i=0;i30;i++)
{
a[i]=rand()%100 + 1;
printf("%d ",a[i]);
}
printf("\n");
int b[30];
for(i=0;i30;i++)
{
b[i]=rand()%100 + 1;
printf("%d ",b[i]);
}
printf("\n");
clock_t start,finish;
double dur;
start= clock();
for(i=0;i30;i++)
{
//printf("輾轉(zhuǎn)相除法所得最大公約數(shù)為:%d\n",gcd(a[i],b[i]));
//printf("窮舉法所得最大公約數(shù)為:%d\n",divisor(a[i],b[i]));
printf("更相減損法所得最大公約數(shù)為:%d\n",gcd2(a[i],b[i]));
//printf("Stein算法所得最大公約數(shù)為:%d\n",Stein(a[i],b[i]));
}
finish=clock();
dur=(double)(finish-start)/CLOCKS_PER_SEC;
printf("運(yùn)行所用的時(shí)間為:%lf s\n",dur);
return 0;
}
分享文章:最小公約數(shù)函數(shù)名稱c語(yǔ)言 最小公約數(shù)計(jì)算c語(yǔ)言
文章轉(zhuǎn)載:http://muchs.cn/article48/dohdohp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、、用戶體驗(yàn)、品牌網(wǎng)站制作、Google、網(wǎng)站營(yíng)銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)