Tell me how to complete the function using recursion?
The program has input data (100 37)
#include <stdio.h>
int rusMult(int a, int b);
int main()
{
int a, b;
scanf("%d%d", &a, &b);
printf("%dn", rusMult(a, b));
return 0;
}
int rusMult(int a, int b)
{
if (b <= 0)
{
}
if (b > 0)
{
int res = rusMult(a * 2, b / 2);
int ost = b % 2;
printf("%dt %dt %dt %dtn", a, b, ost, res);
if (ost == 1)
{
res += a;
}
return rusMult(a * 2, b / 2);
}
}
Sample Input:
100 37
Sample Output:
100 37
200 18
400 9
800 4
1600 2
3200 1
3200
3200 1600 0 3200
3200 800 0 3200
3200 400 1 3600
3600 200 0 3600
3600 100 1 3700
3700
4
Make the function simple:
- If
b < 0
, returnrusMult(a, -b)
. - Otherwise, if
b == 0
, return0
. - Otherwise, if
b
is even, returnrusMult(a*2, b/2)
. - Otherwise, return
rusMult(a*2, b/2) + a
.
You only need eight lines for the function, one for each “If” or “Otherwise”, and one for each return
statement.