📜  递归练习问题套装5

📅  最后修改于: 2021-04-28 14:25:25             🧑  作者: Mango

问题1
预测以下程序的输出。以下fun()通常做什么?

C++
#include 
using namespace std;
int fun(int a, int b)
{
    if (b == 0)
        return 0;
    if (b % 2 == 0)
        return fun(a + a, b/2);
     
    return fun(a + a, b/2) + a;
}
 
int main()
{
    cout << fun(4, 3) ;
    return 0;
}
 
// This code is contributed by SHUBHAMSINGH10


C
#include
 
int fun(int a, int b)
{
   if (b == 0)
       return 0;
   if (b % 2 == 0)
       return fun(a+a, b/2);
 
   return fun(a+a, b/2) + a;
}
 
int main()
{
  printf("%d", fun(4, 3));
  getchar();
  return 0;
}


Java
/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    static int fun(int a, int b)
    {
        if (b == 0)
            return 0;
        if (b % 2 == 0)
            return fun(a + a, b/2);
          
        return fun(a + a, b/2) + a;
    }
  
    public static void main (String[] args)
    {
        System.out.println(fun(4, 3));
    }
}
// This code is contributed by SHUBHAMSINGH10


Python3
def fun(a, b):
    if (b == 0):
        return 0
    if (b % 2 == 0):
        return fun(a + a, b//2)
     
    return fun(a + a, b//2) + a
 
# Driver code
 
print(fun(4, 3))
 
# This code is contributed by SHUBHAMSINGH10


C#
using System;
 
class GFG{
 
    static int fun(int a, int b)
    {
        if (b == 0)
            return 0;
        if (b % 2 == 0)
            return fun(a + a, b/2);
         
        return fun(a + a, b/2) + a;
    }
     
    static public void Main ()
    {
        Console.Write(fun(4, 3));
    }
}
 
// This code is contributed by SHUBHAMSINGH10


C++
#include 
using namespace std;
   
int fun(int a, int b)
{
   if (b == 0)
       return 1;
   if (b % 2 == 0)
       return fun(a*a, b/2);
   
   return fun(a*a, b/2)*a;
}
   
int main()
{
  cout << fun(4, 3) ;
  getchar();
  return 0;
}
 
//This code is contributed by shubhamsingh10


C
#include
 
int fun(int a, int b)
{
   if (b == 0)
       return 1;
   if (b % 2 == 0)
       return fun(a*a, b/2);
 
   return fun(a*a, b/2)*a;
}
 
int main()
{
  printf("%d", fun(4, 3));
  getchar();
  return 0;
}


Java
import java.io.*;
  
class GFG {
    static int fun(int a, int b)
    {
        if (b == 0)
            return 1;
        if (b % 2 == 0)
            return fun(a*a, b/2);
         
        return fun(a*a, b/2)*a;
    }
     
    public static void main (String[] args)
    {
        System.out.println(fun(4, 3));
    }
}
//This code is contributed by shubhamsingh10


Python3
def fun(a, b):
    if (b == 0):
        return 1
    if (b % 2 == 0):
        return fun(a*a, b//2)
     
    return fun(a*a, b//2)*a
  
# Driver code
  
print(fun(4, 3))
 
# This code is contributed by shubhamsingh10


C#
using System;
 
public class GFG{
     
    static int fun(int a, int b)
    {
        if (b == 0)
            return 1;
        if (b % 2 == 0)
            return fun(a*a, b/2);
         
        return fun(a*a, b/2)*a;
    }
     
     
    static public void Main ()
    {
        Console.WriteLine(fun(4, 3));
    }
}
// This code is contributed by shubhamsingh10


C++
#include 
using namespace std;
 
int fun(int n)
{
    if (n > 100)
        return n - 10;
    return fun(fun(n+11));
}
 
int main()
{
    cout << " " << fun(99) << " ";
    getchar();
    return 0;
}
 
// This code is contributed by Shubhamsingh10


C
#include
 
 int fun(int n)
 {
   if (n > 100)
     return n - 10;
   return fun(fun(n+11));
 }
 
int main()
{
  printf(" %d ", fun(99));
  getchar();
  return 0;
}


Java
import java.io.*;
 
class GFG {
    static int fun(int n)
    {
        if (n > 100)
            return n - 10;
        return fun(fun(n+11));
    }
     
    public static void main (String[] args) {
        System.out.println(" " + fun(99) + " ");
    }
}
// This code is contributed by Shubhamsingh10


Python3
def fun(n):
     
    if (n > 100):
        return n - 10
     
    return fun(fun(n + 11))
     
# Driver code
print(fun(99))
 
# This code is contributed by Shubhamsingh10


C#
using System;
 
class GFG{
     
static int fun(int n)
{
    if (n > 100)
        return n - 10;
    return fun(fun(n + 11));
}
 
// Driver code   
static public void Main ()
{
    Console.WriteLine(fun(99));
}
}
 
// This code is contributed by Shubhamsingh10


Javascript


输出:
12

计算a * b(乘以b)。
问题2
在问题1中,如果将+替换为*,并将return 0替换为return 1,那么更改后的函数做什么?以下是更改后的函数。

C++

#include 
using namespace std;
   
int fun(int a, int b)
{
   if (b == 0)
       return 1;
   if (b % 2 == 0)
       return fun(a*a, b/2);
   
   return fun(a*a, b/2)*a;
}
   
int main()
{
  cout << fun(4, 3) ;
  getchar();
  return 0;
}
 
//This code is contributed by shubhamsingh10

C

#include
 
int fun(int a, int b)
{
   if (b == 0)
       return 1;
   if (b % 2 == 0)
       return fun(a*a, b/2);
 
   return fun(a*a, b/2)*a;
}
 
int main()
{
  printf("%d", fun(4, 3));
  getchar();
  return 0;
}

Java

import java.io.*;
  
class GFG {
    static int fun(int a, int b)
    {
        if (b == 0)
            return 1;
        if (b % 2 == 0)
            return fun(a*a, b/2);
         
        return fun(a*a, b/2)*a;
    }
     
    public static void main (String[] args)
    {
        System.out.println(fun(4, 3));
    }
}
//This code is contributed by shubhamsingh10

Python3

def fun(a, b):
    if (b == 0):
        return 1
    if (b % 2 == 0):
        return fun(a*a, b//2)
     
    return fun(a*a, b//2)*a
  
# Driver code
  
print(fun(4, 3))
 
# This code is contributed by shubhamsingh10

C#

using System;
 
public class GFG{
     
    static int fun(int a, int b)
    {
        if (b == 0)
            return 1;
        if (b % 2 == 0)
            return fun(a*a, b/2);
         
        return fun(a*a, b/2)*a;
    }
     
     
    static public void Main ()
    {
        Console.WriteLine(fun(4, 3));
    }
}
// This code is contributed by shubhamsingh10
输出:
64

它计算a ^ b(从a升到b的幂)。
问题3
预测以下程序的输出。以下fun()通常做什么?

C++

#include 
using namespace std;
 
int fun(int n)
{
    if (n > 100)
        return n - 10;
    return fun(fun(n+11));
}
 
int main()
{
    cout << " " << fun(99) << " ";
    getchar();
    return 0;
}
 
// This code is contributed by Shubhamsingh10

C

#include
 
 int fun(int n)
 {
   if (n > 100)
     return n - 10;
   return fun(fun(n+11));
 }
 
int main()
{
  printf(" %d ", fun(99));
  getchar();
  return 0;
}

Java

import java.io.*;
 
class GFG {
    static int fun(int n)
    {
        if (n > 100)
            return n - 10;
        return fun(fun(n+11));
    }
     
    public static void main (String[] args) {
        System.out.println(" " + fun(99) + " ");
    }
}
// This code is contributed by Shubhamsingh10

Python3

def fun(n):
     
    if (n > 100):
        return n - 10
     
    return fun(fun(n + 11))
     
# Driver code
print(fun(99))
 
# This code is contributed by Shubhamsingh10

C#

using System;
 
class GFG{
     
static int fun(int n)
{
    if (n > 100)
        return n - 10;
    return fun(fun(n + 11));
}
 
// Driver code   
static public void Main ()
{
    Console.WriteLine(fun(99));
}
}
 
// This code is contributed by Shubhamsingh10

Java脚本


输出:
91
fun(99) = fun(fun(110)) since 99 ? 100
           = fun(100)    since 110 > 100
           = fun(fun(111)) since 100 ? 100
           = fun(101)    since 111 > 100
           = 91        since 101 > 100