设a、b都是自然数,为求a除以b的余数,某人编写了以下函数:Function fun(a As Integer,b As Integer)While a>ba=a-bWendfun=aEnd Function在调试时发现函数是错误的。为使函数能产生正确的返回值,应做的修改是A.把a=a-b改为a=b-aB.把a=a-b改为a=a\bC.把While a>b改为While a<bD.把While a>b改为While a>=b

题目

设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


相似考题
更多“设a、b都是自然数,为求a除以b的余数,某人编写了以下函数:Function fun(a As Integer,b As Integer ”相关问题
  • 第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


    正确答案:D
    当a=b时,余数应该为0,而不是a,程序执行结果会跳出循环使fun=a,应该继续循环,正确答案为D。

  • 第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

    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的商的整数部分


    正确答案:B
    B。【解析】本题考查了DoLoop循环,实现的是最小公倍数。本题中If语句实现了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;