设a、b都是自然数,为求a除以b的余数,某人编写了以下函数: Function fun(a As Integer,b As Integer) While a>b a=a-b Wend fun=a End Function 在调试时发现函数是错误的。为使函数

admin2019-01-17  24

问题 设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

答案D

解析 本题函数过程的原代码仅适合a不能被b整除的两数,而当a能被b整除时则不适用,如a=4,b=2,a除以b的余数为0,而原代码返回的函数值为2。因此需将语句While a>b改为While a>=b。
转载请注明原文地址:https://kaotiyun.com/show/nMFp777K
0

最新回复(0)