原文:

给定两个数字 n 和 m,任务是用递归求这两个数字的乘积。 :数字可以是正数,也可以是负数。

示例:

input : n = 5 ,  m = 3
output : 15
input : n = 5  ,  m = -3
output : -15
input : n = -5  ,  m = 3
output : -15
input : n = -5  ,  m = -3
output:15

只有正数的上述问题的递归pg电子试玩链接的解决方案已经在中讨论过。在这篇文章中,讨论了求正数和负数乘积的递归解法。

下面是一步一步的方法:

  1. 检查一个或两个数字是否为负数。
  2. 如果在第二个参数中传递的数字是负数,交换参数并再次调用函数。
  3. 如果两个参数都是负数,则再次调用该函数,并将数字的绝对值作为参数传递。
  4. 如果 n>m,调用带有交换参数的函数,以减少函数的执行时间。
  5. 只要 m 不为 0,就继续用子基 n,m-1 调用函数,并返回 n multi recury(n,m-1)。

下面是上述方法的实现:

c

// c   program to find product of two numbers
// using recursion
#include 
using namespace std;
// recursive function to calculate the product
// of 2 integers
int multrecur(int n, int m)
{
    // case 1 : n<0 and m>0
    // swap the position of n and m to keep second
    // parameter positive
    if (n > 0 && m < 0) {
        return multrecur(m, n);
    }
    // case 2 : both n and m are less than 0
    // return the product of their absolute values
    else if (n < 0 && m < 0) {
        return multrecur((-1 * n), (-1 * m));
    }
    // if n>m , swap n and m so that recursion
    // takes less time
    if (n > m) {
        return multrecur(m, n);
    }
    // as long as m is not 0 recursively call multrecur for 
    // n and m-1 return sum of n and the product of n times m-1
    else if (m != 0) {
        return n   multrecur(n, m - 1);
    }
    // m=0 then return 0
    else {
        return 0;
    }
}
// driver code
int main()
{
    cout << "5 * 3 = " << multrecur(5, 3) << endl;
    cout << "5 * (-3) = " << multrecur(5, -3) << endl;
    cout << "(-5) * 3 = " << multrecur(-5, 3) << endl;
    cout << "(-5) * (-3) = " << multrecur(-5, -3) << endl;
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

//java program to find product of two numbers
//using recursion
public class gfg {
    //recursive function to calculate the product
    //of 2 integers
    static int multrecur(int n, int m)
    {
    // case 1 : n<0 and m>0
    // swap the position of n and m to keep second
    // parameter positive
    if (n > 0 && m < 0) {
        return multrecur(m, n);
    }
    // case 2 : both n and m are less than 0
    // return the product of their absolute values
    else if (n < 0 && m < 0) {
        return multrecur((-1 * n), (-1 * m));
    }
    // if n>m , swap n and m so that recursion
    // takes less time
    if (n > m) {
        return multrecur(m, n);
    }
    // as long as m is not 0 recursively call multrecur for 
    // n and m-1 return sum of n and the product of n times m-1
    else if (m != 0) {
        return n   multrecur(n, m - 1);
    }
    // m=0 then return 0
    else {
        return 0;
    }
    }
    //driver code
    public static void main(string[] args) {
        system.out.println("5 * 3 = "   multrecur(5, 3));
        system.out.println("5 * (-3) = "   multrecur(5, -3));
        system.out.println("(-5) * 3 = "   multrecur(-5, 3));
        system.out.println("(-5) * (-3) = "  multrecur(-5, -3));
    }
}

python 3

# python 3 program to find product of two numbers
# using recursion
# recursive function to calculate the product
# of 2 integers
def multrecur(n, m) :
    # case 1 : n<0 and m>0
    # swap the position of n and m to keep second
    # parameter positive
    if n > 0 and m < 0 :
        return multrecur(m,n)
    # case 2 : both n and m are less than 0
    # return the product of their absolute values
    elif n < 0 and m < 0 :
        return multrecur((-1 * n),(-1 * m))
    # if n>m , swap n and m so that recursion
    # takes less time
    if n > m :
        return multrecur(m, n)
    # as long as m is not 0 recursively call multrecur for 
    # n and m-1 return sum of n and the product of n times m-1
    elif m != 0 :
        return n   multrecur(n, m-1)
    # m=0 then return 0
    else :
        return 0
# driver code
if __name__ == "__main__" :
    print("5 * 3 =",multrecur(5, 3))
    print("5 * (-3) =",multrecur(5, -3))
    print("(-5) * 3 =",multrecur(-5, 3))
    print("(-5) * (-3) =",multrecur(-5, -3))
# this code is contributed by ankitrai1

c

// c# program to find product of
// two numbers using recursion
using system;
class gfg
{
// recursive function to calculate
// the product of 2 integers
static int multrecur(int n, int m)
{
// case 1 : n<0 and m>0
// swap the position of n and m
// to keep second parameter positive
if (n > 0 && m < 0)
{
    return multrecur(m, n);
}
// case 2 : both n and m are less than 0
// return the product of their absolute values
else if (n < 0 && m < 0)
{
    return multrecur((-1 * n), (-1 * m));
}
// if n>m , swap n and m so that
// recursion takes less time
if (n > m)
{
    return multrecur(m, n);
}
// as long as m is not 0 recursively
// call multrecur for n and m-1 return
// sum of n and the product of n times m-1
else if (m != 0)
{
    return n   multrecur(n, m - 1);
}
// m=0 then return 0
else
{
    return 0;
}
}
// driver code
public static void main()
{
    console.writeline("5 * 3 = "  
                       multrecur(5, 3));
    console.writeline("5 * (-3) = "  
                       multrecur(5, -3));
    console.writeline("(-5) * 3 = "  
                      multrecur(-5, 3));
    console.writeline("(-5) * (-3) = "  
                      multrecur(-5, -3));
}
}
// this code is contributed by anuj_67

服务器端编程语言(professional hypertext preprocessor 的缩写)

0
    // swap the position of n and m to keep second
    // parameter positive
    if ($n > 0 && $m < 0)
    {
        return multrecur($m, $n);
    }
    // case 2 : both n and m are less than 0
    // return the product of their absolute values
    else if ($n < 0 && $m < 0)
    {
        return multrecur((-1 * $n),
                        (-1 * $m));
    }
    // if n>m , swap n and m so that
    // recursion takes less time
    if ($n > $m)
    {
        return multrecur($m, $n);
    }
    // as long as m is not 0 recursively call multrecur for 
    // n and m-1 return sum of n and the product of n times m-1
    else if ($m != 0)
    {
        return $n   multrecur($n, $m - 1);
    }
    // m=0 then return 0
    else
    {
        return 0;
    }
}
// driver code
echo "5 * 3 = " . multrecur(5, 3) . "\n";
echo "5 * (-3) = " . multrecur(5, -3) . "\n";
echo "(-5) * 3 = " . multrecur(-5, 3) . "\n";
echo "(-5) * (-3) = " . multrecur(-5, -3) . "\n";
// this code is contributed by mits
?>

java 描述语言


output: 

5 * 3 = 15
5 * (-3) = -15
(-5) * 3 = -15
(-5) * (-3) = 15

时间复杂度: o(max(n,m)) 辅助空间: o(max(n,m))