📜  使用递归的2个数字的乘积|套装2

📅  最后修改于: 2021-04-27 05:22:57             🧑  作者: Mango

给定两个数字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

在前一篇文章中已经讨论了仅正数的上述问题的递归解决方案。在这篇文章中,讨论了寻找正负数乘积的递归解决方案。

以下是逐步方法:

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

下面是上述方法的实现:

C++
// CPP 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


PHP
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
?>


输出:
5 * 3 = 15
5 * (-3) = -15
(-5) * 3 = -15
(-5) * (-3) = 15