欢迎来到天天文库
浏览记录
ID:14145022
大小:42.50 KB
页数:6页
时间:2018-07-26
《求最大公约数递归算法》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、求最大公约数递归算法#includeintgcd(intm,intn){intg;g=m%n;if(0==g){returnn;}else{returngcd(n,g);}}intmain(){intm,n;scanf("%d,%d",&m,&n);printf("gcd=%d",gcd(m,n));return0;}求最大公约数辗转相除法(1)#includeintgcd(intm,intn){intr,t;//若m2、大公约数,否则递归if(n==0)returnm;elsereturngcd(m,n);//递归是要return的,此处漏了}intmain(){inta;intb;scanf("%d%d",&a,&b);intresult=gcd(a,b);printf("%d",result);getch();return0;}(2)#includeintgcd(intm,intn){intr,t;if(m3、a;intb;scanf("%d%d",&a,&b);intresult=gcd(a,b);printf("%d",result);getchar();return0;}intfun2(intx,inty,intz){returny;}intfun3(intx,inty,intz){returnz;}求最小公倍数递归算法(1)privateintgetNum(Int32[]num){if(num.Count()>1){intint1=num[0];Int32[]num1=newInt32[num.Length-1];for(inti=1;i4、th;i++){num1[i-1]=num[i];}num1[0]=int1*num1[0]/LCM(int1,num1[0]);returngetNum(num1);}else{returnnum[0];}}//最大公约数privateintLCM(intm,intn){if(m%n==05、6、n%m==0){if(m7、intn=20;Console.WriteLine("最大公约数:{0}",LCM(m,n));//两个正整数的最小公倍数=两个数的乘积÷两个数的最大公约数Console.WriteLine("最小公倍数:{0}",m*n/LCM(m,n));}staticintLCM(intm,intn){if(m%n==08、9、n%m==0){if(m10、nputmandn:");scanf("%d,%d",&m,&n);//赋值m,nif(nmain(){inta,b,c,d;scanf("%d%d",&a,&b);/*假设,输入011、=b;fprintf("最小公倍数为%d",d);}(3)intmaxcomdivisor(inta,intb){while(a%b!=0){intc=a%b;a=b;b=c;}returnb;}intmincomdivor(inta,intb){intd=maxcomdivisor(a,b);d=a*b/d;returnd;}voidmain(){intmaxcomdivisor(int,int);intmincomdivor(int,int);inta,b;printf("请输入两个正整数:");scanf("%d,%d",&a,&b);printf("12、%d与%d的最大公约数是
2、大公约数,否则递归if(n==0)returnm;elsereturngcd(m,n);//递归是要return的,此处漏了}intmain(){inta;intb;scanf("%d%d",&a,&b);intresult=gcd(a,b);printf("%d",result);getch();return0;}(2)#includeintgcd(intm,intn){intr,t;if(m3、a;intb;scanf("%d%d",&a,&b);intresult=gcd(a,b);printf("%d",result);getchar();return0;}intfun2(intx,inty,intz){returny;}intfun3(intx,inty,intz){returnz;}求最小公倍数递归算法(1)privateintgetNum(Int32[]num){if(num.Count()>1){intint1=num[0];Int32[]num1=newInt32[num.Length-1];for(inti=1;i4、th;i++){num1[i-1]=num[i];}num1[0]=int1*num1[0]/LCM(int1,num1[0]);returngetNum(num1);}else{returnnum[0];}}//最大公约数privateintLCM(intm,intn){if(m%n==05、6、n%m==0){if(m7、intn=20;Console.WriteLine("最大公约数:{0}",LCM(m,n));//两个正整数的最小公倍数=两个数的乘积÷两个数的最大公约数Console.WriteLine("最小公倍数:{0}",m*n/LCM(m,n));}staticintLCM(intm,intn){if(m%n==08、9、n%m==0){if(m10、nputmandn:");scanf("%d,%d",&m,&n);//赋值m,nif(nmain(){inta,b,c,d;scanf("%d%d",&a,&b);/*假设,输入011、=b;fprintf("最小公倍数为%d",d);}(3)intmaxcomdivisor(inta,intb){while(a%b!=0){intc=a%b;a=b;b=c;}returnb;}intmincomdivor(inta,intb){intd=maxcomdivisor(a,b);d=a*b/d;returnd;}voidmain(){intmaxcomdivisor(int,int);intmincomdivor(int,int);inta,b;printf("请输入两个正整数:");scanf("%d,%d",&a,&b);printf("12、%d与%d的最大公约数是
3、a;intb;scanf("%d%d",&a,&b);intresult=gcd(a,b);printf("%d",result);getchar();return0;}intfun2(intx,inty,intz){returny;}intfun3(intx,inty,intz){returnz;}求最小公倍数递归算法(1)privateintgetNum(Int32[]num){if(num.Count()>1){intint1=num[0];Int32[]num1=newInt32[num.Length-1];for(inti=1;i4、th;i++){num1[i-1]=num[i];}num1[0]=int1*num1[0]/LCM(int1,num1[0]);returngetNum(num1);}else{returnnum[0];}}//最大公约数privateintLCM(intm,intn){if(m%n==05、6、n%m==0){if(m7、intn=20;Console.WriteLine("最大公约数:{0}",LCM(m,n));//两个正整数的最小公倍数=两个数的乘积÷两个数的最大公约数Console.WriteLine("最小公倍数:{0}",m*n/LCM(m,n));}staticintLCM(intm,intn){if(m%n==08、9、n%m==0){if(m10、nputmandn:");scanf("%d,%d",&m,&n);//赋值m,nif(nmain(){inta,b,c,d;scanf("%d%d",&a,&b);/*假设,输入011、=b;fprintf("最小公倍数为%d",d);}(3)intmaxcomdivisor(inta,intb){while(a%b!=0){intc=a%b;a=b;b=c;}returnb;}intmincomdivor(inta,intb){intd=maxcomdivisor(a,b);d=a*b/d;returnd;}voidmain(){intmaxcomdivisor(int,int);intmincomdivor(int,int);inta,b;printf("请输入两个正整数:");scanf("%d,%d",&a,&b);printf("12、%d与%d的最大公约数是
4、th;i++){num1[i-1]=num[i];}num1[0]=int1*num1[0]/LCM(int1,num1[0]);returngetNum(num1);}else{returnnum[0];}}//最大公约数privateintLCM(intm,intn){if(m%n==0
5、
6、n%m==0){if(m7、intn=20;Console.WriteLine("最大公约数:{0}",LCM(m,n));//两个正整数的最小公倍数=两个数的乘积÷两个数的最大公约数Console.WriteLine("最小公倍数:{0}",m*n/LCM(m,n));}staticintLCM(intm,intn){if(m%n==08、9、n%m==0){if(m10、nputmandn:");scanf("%d,%d",&m,&n);//赋值m,nif(nmain(){inta,b,c,d;scanf("%d%d",&a,&b);/*假设,输入011、=b;fprintf("最小公倍数为%d",d);}(3)intmaxcomdivisor(inta,intb){while(a%b!=0){intc=a%b;a=b;b=c;}returnb;}intmincomdivor(inta,intb){intd=maxcomdivisor(a,b);d=a*b/d;returnd;}voidmain(){intmaxcomdivisor(int,int);intmincomdivor(int,int);inta,b;printf("请输入两个正整数:");scanf("%d,%d",&a,&b);printf("12、%d与%d的最大公约数是
7、intn=20;Console.WriteLine("最大公约数:{0}",LCM(m,n));//两个正整数的最小公倍数=两个数的乘积÷两个数的最大公约数Console.WriteLine("最小公倍数:{0}",m*n/LCM(m,n));}staticintLCM(intm,intn){if(m%n==0
8、
9、n%m==0){if(m10、nputmandn:");scanf("%d,%d",&m,&n);//赋值m,nif(nmain(){inta,b,c,d;scanf("%d%d",&a,&b);/*假设,输入011、=b;fprintf("最小公倍数为%d",d);}(3)intmaxcomdivisor(inta,intb){while(a%b!=0){intc=a%b;a=b;b=c;}returnb;}intmincomdivor(inta,intb){intd=maxcomdivisor(a,b);d=a*b/d;returnd;}voidmain(){intmaxcomdivisor(int,int);intmincomdivor(int,int);inta,b;printf("请输入两个正整数:");scanf("%d,%d",&a,&b);printf("12、%d与%d的最大公约数是
10、nputmandn:");scanf("%d,%d",&m,&n);//赋值m,nif(nmain(){inta,b,c,d;scanf("%d%d",&a,&b);/*假设,输入011、=b;fprintf("最小公倍数为%d",d);}(3)intmaxcomdivisor(inta,intb){while(a%b!=0){intc=a%b;a=b;b=c;}returnb;}intmincomdivor(inta,intb){intd=maxcomdivisor(a,b);d=a*b/d;returnd;}voidmain(){intmaxcomdivisor(int,int);intmincomdivor(int,int);inta,b;printf("请输入两个正整数:");scanf("%d,%d",&a,&b);printf("12、%d与%d的最大公约数是
11、=b;fprintf("最小公倍数为%d",d);}(3)intmaxcomdivisor(inta,intb){while(a%b!=0){intc=a%b;a=b;b=c;}returnb;}intmincomdivor(inta,intb){intd=maxcomdivisor(a,b);d=a*b/d;returnd;}voidmain(){intmaxcomdivisor(int,int);intmincomdivor(int,int);inta,b;printf("请输入两个正整数:");scanf("%d,%d",&a,&b);printf("
12、%d与%d的最大公约数是
此文档下载收益归作者所有