设a、b都是自然数,为求a除以b的余数,某人编写了以下函数:
Function fun(a As Integer,b As Integer)
While a>b
a=a-b
Wend
fun=a
End Function
在调试时发现函数是错误的。为使函数能产生正确的返回值,应做的修改是
A.把a=a-b改为a=b-a
B.把a=a-b改为a=a\b
C.把While a>b改为While a<b
D.把While a>b改为While a>=b
第1题:
设a、b都是自然数,为求a除以b的余数,某人编写了以下函数: Eunction fun(a As Integer,b As Integer) While a>b a=a—b Wend fun=a End Function 在调试时发现函数是错误的。为使函数能产生正确的返回值,应做的修改是( )。
A.把a=a-b改为a=b-a
B.把a=a-b改为a=a\b
C.把While a>b改为While a%b
D.把While a>b改为While a>=b
第2题:
求两数的最大公约数
function gcd(a,b:integer):integer;
begin
if b=0 then gcd:=a
else gcd:=gcd (b,a mod b);
end ;
第3题:
设有以下函数过程:
Function fun(a As Integer,b As Integer)
Dim c As Integer
If a<b Then
c=a:a=b:b=C
End IF
c=0
Do
c=c+a
Loop Until c Mod b=0
fun=c
End Function
若调用函数fun时的实际参数都是自然数,则函数返回的是( )。
A.a、b的最大公约数
B.a、b的最小公倍数
C.a除以b的余数
D.a除以b的商的整数部分
B。【解析】本题考查了Do Loop循环,实现的是最小公倍数。本题中If语句实现了a和b交换。
第4题:
设有以下函数过程: Function fun(a As Integer,b As Integer) Dim c As Integer If a<b Then c=a:a=b:b=C End IF c=0 Do c=c+a Loop Until c Mod b=0 fun=c End Function 若调用函数fun时的实际参数都是自然数,则函数返回的是( )。
A.a、b的最大公约数
B.a、b的最小公倍数
C.a除以b的余数
D.a除以b的商的整数部分
第5题:
求两数的最小公倍数
function lcm(a,b:integer):integer;
begin
if a<b then swap(a,b);
lcm:=a;
while lcm mod b>0 do inc(lcm,a);
end;