📜  递归练习问题套装3

📅  最后修改于: 2021-05-04 11:44:08             🧑  作者: Mango

解释以下递归函数的功能。

问题1

C++
void fun1(int n)
{
   int i = 0;  
   if (n > 1)
     fun1(n - 1);
   for (i = 0; i < n; i++)
     cout << " * ";
}
 
// This code is contributed by shubhamsingh10


C
void fun1(int n)
{
   int i = 0; 
   if (n > 1)
     fun1(n-1);
   for (i = 0; i < n; i++)
     printf(" * ");
}


Java
static void fun1(int n)
{
   int i = 0;  
   if (n > 1)
     fun1(n - 1);
   for (i = 0; i < n; i++)
     System.out.print(" * ");
}
  
// This code is contributed by shubhamsingh10


Python3
def  fun1(n):
    i = 0
    if (n > 1):
        fun1(n - 1)
    for i in range(n):
        print(" * ",end="")
 
# This code is contributed by shubhamsingh10


C#
static void fun1(int n)
{
    int i = 0;
    if (n > 1)
        fun1(n-1);
    for (i = 0; i < n; i++)
        Console.Write(" * ");
}
 
// This code is contributed by shubhamsingh10


Javascript


C++
#define LIMIT 1000
void fun2(int n)
{
  if (n <= 0)
     return;
  if (n > LIMIT)
    return;
  cout << n <<" ";
  fun2(2*n);
  cout << n <<" ";
}
 
// This code is contributed by shubhamsingh10


C
#define LIMIT 1000
void fun2(int n)
{
  if (n <= 0)
     return;
  if (n > LIMIT)
    return;
  printf("%d ", n);
  fun2(2*n);
  printf("%d ", n);
}


Java
int LIMIT = 1000;
void fun2(int n)
{
    if (n <= 0) return;
    if (n > LIMIT) return;
 
    System.out.print(String.format("%d ", n));
    fun2(2 * n);
    System.out.print(String.format("%d ", n));
}


Python3
LIMIT = 1000
def fun2(n):
    if (n <= 0):
        return
    if (n > LIMIT):
        return
    print(n, end=" ")
    fun2(2 * n)
    print(n, end=" ")
 
# This code is contributed by shubhamsingh10


C#
int LIMIT = 1000
void fun2(int n)
{
    if (n <= 0)
        return;
    if (n > LIMIT)
        return;
    Console.Write(n+" ");
    fun2(2*n);
    Console.Write(n+" ");
}
 
// This code is contributed by Shubhamsingh10


Javascript


答:打印的星星总数等于1 + 2 +…。 (n-2)+(n-1)+ n,即n(n + 1)/ 2。

问题2

C++

#define LIMIT 1000
void fun2(int n)
{
  if (n <= 0)
     return;
  if (n > LIMIT)
    return;
  cout << n <<" ";
  fun2(2*n);
  cout << n <<" ";
}
 
// This code is contributed by shubhamsingh10

C

#define LIMIT 1000
void fun2(int n)
{
  if (n <= 0)
     return;
  if (n > LIMIT)
    return;
  printf("%d ", n);
  fun2(2*n);
  printf("%d ", n);
}  

Java

int LIMIT = 1000;
void fun2(int n)
{
    if (n <= 0) return;
    if (n > LIMIT) return;
 
    System.out.print(String.format("%d ", n));
    fun2(2 * n);
    System.out.print(String.format("%d ", n));
}

Python3

LIMIT = 1000
def fun2(n):
    if (n <= 0):
        return
    if (n > LIMIT):
        return
    print(n, end=" ")
    fun2(2 * n)
    print(n, end=" ")
 
# This code is contributed by shubhamsingh10

C#

int LIMIT = 1000
void fun2(int n)
{
    if (n <= 0)
        return;
    if (n > LIMIT)
        return;
    Console.Write(n+" ");
    fun2(2*n);
    Console.Write(n+" ");
}
 
// This code is contributed by Shubhamsingh10

Java脚本