下列计算最大公约数的递归过程,正确的是___________。
A: Sub gcd(ByVal m%, ByVal n%) Dim r% r = m n If r = 0 Then gcd = n Else gcd = gcd(n, r) End If End Sub
B: Sub gcd(ByVal m%, ByVal n%) Dim r% r = m Mod n If r = 0 Then gcd = n Else gcd = gcd(n, r) End If End Sub
C: Function gcd%(ByVal m%, ByVal n%) Dim r% r = m n If r = 0 Then gcd = n Else gcd = gcd(n, r) End If End Function
D: Function gcd%(ByVal m%, ByVal n%) Dim r% r = m Mod n If r = 0 Then gcd = n Else gcd = gcd(n, r) End If End Function
A: Sub gcd(ByVal m%, ByVal n%) Dim r% r = m n If r = 0 Then gcd = n Else gcd = gcd(n, r) End If End Sub
B: Sub gcd(ByVal m%, ByVal n%) Dim r% r = m Mod n If r = 0 Then gcd = n Else gcd = gcd(n, r) End If End Sub
C: Function gcd%(ByVal m%, ByVal n%) Dim r% r = m n If r = 0 Then gcd = n Else gcd = gcd(n, r) End If End Function
D: Function gcd%(ByVal m%, ByVal n%) Dim r% r = m Mod n If r = 0 Then gcd = n Else gcd = gcd(n, r) End If End Function
举一反三
- 求给定自然数m和n的最大公约数。请完善程序。 Private Sub Command1_Click() Dim m As Integer, n As Integer, r As Integer m = Text1.Text n = Text2.Text If m < n Then r = m: m = n: n = r Do r = m Mod n m = n n = r Loop Until__________ Text3.Text = m End Sub Private Sub Command2_Click() Dim m As Integer, n As Integer, r As Integer m = Text1.Text n = Text2.Text If m < n Then r = m: m = n: n = r r = m Mod n Do Until _______ m = n n = r r = m Mod n Loop Text3.Text =____ End Sub Private Sub Command3_Click() Dim m As Integer, n As Integer, r As Integer m = Text1.Text n = Text2.Text If m < n Then r = m: m = n: n = r Do r = m Mod n m = n n = r Loop While________ Text3.Text =______ End Sub Private Sub Command4_Click() Dim m As Integer, n As Integer, r As Integer m = Text1.Text n = Text2.Text If m < n Then r = m: m = n: n = r r = m Mod n Do While______ m = n n = r r = m Mod n Loop Text3.Text =____ End Sube73f1fd63959cc8362d8c82d419c2353.png
- 将十进制正整数d转换成r(2~9)进制的字符串,下列递归函数正确的是__________。 A: Function Trans$(ByVal d%, ByVal r%) If d = 0 Then Trans = "" Else Trans = Trans(d \ r, r) & (d Mod r) End If End Function B: Function Trans$(ByVal d%, ByVal r%) If d = 0 Then Trans = "" Else Trans = (d Mod r) & Trans(d \ r, r) End If End Function C: Function Trans$(ByVal d%, ByVal r%) If d = 0 Then Trans = Trans(d \ r, r) & (d Mod r) Else Trans = "" End If End Function D: Function Trans$(ByVal d%, ByVal r%) If d = 0 Then Trans = (d Mod r) & Trans(d \ r, r) Else Trans = "" End If End Function
- 有以下递归函数求最大公约数的程序, 则程序的执行结果是( )。#include<stdio.h>int gcd(int m, int n){ int g; if (n == 0) g=m; else g=gcd(n,m%n); return (g); }void main( ){ int m=21,n=7; printf(“%d”,gcd(m,n)); }
- 下列程序的作用是求两个正整数m,n的最大公约数,请补充程序。def gcd(m,n): if m<n: m,n=n,m if m%n==0: ① else: return ② ans=gcd(84,342)print(ans)
- 编写用辗转相除法求两个数最大公约数的函数,调用该函数求两个数的最大公约数和最小公倍数。 #include "stdio.h" int gcd(int m,int n) { int r; do {【1】; m=n; n=r; } while(r!=0); return 【2】 ; } main() { int a,b,x,y; scanf("%d%d",&a,&b); x=gcd(【3】); y=a*b/x; /*求a和b的最小公倍数*/ printf("GCD=%d,LCM=%d\n",x,y); }