標(biāo)題:
c語言 輾轉(zhuǎn)相除法求兩個正整數(shù)最大公約數(shù)
[打印本頁]
作者:
liuda
時間:
2015-1-22 02:46
標(biāo)題:
c語言 輾轉(zhuǎn)相除法求兩個正整數(shù)最大公約數(shù)
#include<stdio.h>//輾轉(zhuǎn)相除法求兩個正整數(shù)最大公約數(shù),間接最小公倍數(shù)
void main()
{
int m,n,max,min,p,r;
printf("please input two integers:(use comma to separate them)\n");
scanf("%d,%d",&m,&n);
if(m>n)
{
p=n;
n=m;//有注釋的程序容易被看懂,因為說的是人話,機(jī)器還沒理解到那種程度,如果誰要是編一個中國人用的漢字程序就好了
m=p;//這一部分的作用就是n大m小
}
max=n;
min=m;
while(m!=0)//這里只能是m,因為r還沒算出來
{
r=n%m;
n=m;
m=r;
}
printf("their greatest common divisor:%d\n",n);
printf("their leatest common multiple:%d\n",max*min/n);
}
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1