📜  递归练习问题套装6

📅  最后修改于: 2021-04-28 17:27:34             🧑  作者: Mango

问题1
考虑下面的递归C函数。设len为字符串s的长度, num为屏幕上打印的字符数,给出numlen之间的关系,其中len始终大于0。

C++
void abc(char *s)
{
    if(s[0] == '\0')
        return;
  
    abc(s + 1);
    abc(s + 1);
    cout << s[0];   
}
 
// This code is contributed by shubhamsingh10


C
void abc(char *s)
{
    if(s[0] == '\0')
        return;
 
    abc(s + 1);
    abc(s + 1);
    printf("%c", s[0]);   
}


Java
static void abc(char *s)
{
    if(s[0] == '\0')
        return;
   
    abc(s + 1);
    abc(s + 1);
    System.out.print(s[0]);   
}
 
// This code is contributed by shubhamsingh10


Python3
def abc(s):
    if(len(s) == 0):
        return
     
    abc(s[1:])
    abc(s[1:])
    print(s[0])
     
# This code is contributed by shubhamsingh10


C#
static void abc(char *s)
{
    if(s[0] == '\0')
        return;
 
    abc(s + 1);
    abc(s + 1);
    Console.Write(s[0]);
}
 
// This code is contributed by shubhamsingh10


C++
#include 
using namespace std;
 
void abc(char s[])
{
    if(s[0] == '\0')
        return;
 
    abc(s + 1);
    abc(s + 1);
    cout << s[0];
}
 
int main()
{
    abc("xyz");
    return 0;
}
//This code is contributed by shubhamsingh10


C
#include
 
void abc(char *s)
{
    if(s[0] == '\0')
        return;
 
    abc(s + 1);
    abc(s + 1);
    printf("%c", s[0]);
}
 
int main()
{
    abc("xyz");
    return 0;
}


Java
public class GFG
{
    static void abc(String s)
    {
        if(s.length() == 0)
            return;
      
        abc(s.substring(1));
        abc(s.substring(1));
        System.out.print(s.charAt(0));
    }
 
    public static void main(String[] args) {
        abc("xyz");
    }
}
 
// This code is contributed by divyeh072019


Python3
def abc(s):
    if(len(s) == 0):
        return
     
    abc(s[1:])
    abc(s[1:])
    print(s[0],end="")
 
 
# Driver code
 
abc("xyz")
 
# This code is contributed by shubhamsingh10


C#
using System;
class GFG {
     
    static void abc(string s)
    {
        if(s.Length == 0)
            return;
             
        abc(s.Substring(1));
        abc(s.Substring(1));
        Console.Write(s[0]);
    }
 
  // Driver code
  static void Main() {
    abc("xyz");
  }
}
 
// This code is contributed by divyeshrabadiya07


C++
#include 
using namespace std;
 
int fun(int count)
{
    cout << count << endl;
    if(count < 3)
    {
        fun(fun(fun(++count)));
    }
    return count;
}
 
int main()
{
    fun(1);
    return 0;
}
 
// This code is contributed by Shubhamsingh10


C
#include
int fun(int count)
{
    printf("%d\n", count);
    if(count < 3)
    {
      fun(fun(fun(++count)));
    }
    return count;
}
 
int main()
{
    fun(1);
    return 0;
}


Java
import java.util.*;
  
class GFG{
static int fun(int count)
{
    System.out.println(count);
    if(count < 3)
    {
        fun(fun(fun(++count)));
    }
    return count;
}
 
public static void main(String[] args)
{
    fun(1);
}
}
 
// This code is contributed by Shubhamsingh10


Python3
def fun(count):
    print(count)
    if(count < 3):
        count+=1
        fun(fun(fun(count)))
     
    return count
  
 
if __name__=="__main__": 
     
    fun(1)
 
# This code is contributed by Shubhamsingh10


C#
using System;
 
class GFG{
     
    static int fun(int count) 
    { 
        Console.Write(count+"\n"); 
        if(count < 3) 
        { 
            fun(fun(fun(++count))); 
        } 
        return count; 
    } 
       
    static public void Main ()
    { 
        fun(1);  
    }
}
 
// This code is contributed by shubhamsingh10


Javascript


以下是numlen之间的关系。

num = 2^len-1
s[0] is 1 time printed
s[1] is 2 times printed
s[2] is 4 times printed
s[i] is printed 2^i times
s[strlen(s)-1] is printed 2^(strlen(s)-1) times
total = 1+2+....+2^(strlen(s)-1)
      = (2^strlen(s)) - 1

例如,以下程序打印7个字符。

C++

#include 
using namespace std;
 
void abc(char s[])
{
    if(s[0] == '\0')
        return;
 
    abc(s + 1);
    abc(s + 1);
    cout << s[0];
}
 
int main()
{
    abc("xyz");
    return 0;
}
//This code is contributed by shubhamsingh10

C

#include
 
void abc(char *s)
{
    if(s[0] == '\0')
        return;
 
    abc(s + 1);
    abc(s + 1);
    printf("%c", s[0]);
}
 
int main()
{
    abc("xyz");
    return 0;
}

Java

public class GFG
{
    static void abc(String s)
    {
        if(s.length() == 0)
            return;
      
        abc(s.substring(1));
        abc(s.substring(1));
        System.out.print(s.charAt(0));
    }
 
    public static void main(String[] args) {
        abc("xyz");
    }
}
 
// This code is contributed by divyeh072019

Python3

def abc(s):
    if(len(s) == 0):
        return
     
    abc(s[1:])
    abc(s[1:])
    print(s[0],end="")
 
 
# Driver code
 
abc("xyz")
 
# This code is contributed by shubhamsingh10

C#

using System;
class GFG {
     
    static void abc(string s)
    {
        if(s.Length == 0)
            return;
             
        abc(s.Substring(1));
        abc(s.Substring(1));
        Console.Write(s[0]);
    }
 
  // Driver code
  static void Main() {
    abc("xyz");
  }
}
 
// This code is contributed by divyeshrabadiya07

感谢bharat nag提出了此解决方案。
问题2

C++

#include 
using namespace std;
 
int fun(int count)
{
    cout << count << endl;
    if(count < 3)
    {
        fun(fun(fun(++count)));
    }
    return count;
}
 
int main()
{
    fun(1);
    return 0;
}
 
// This code is contributed by Shubhamsingh10

C

#include
int fun(int count)
{
    printf("%d\n", count);
    if(count < 3)
    {
      fun(fun(fun(++count)));
    }
    return count;
}
 
int main()
{
    fun(1);
    return 0;
}

Java

import java.util.*;
  
class GFG{
static int fun(int count)
{
    System.out.println(count);
    if(count < 3)
    {
        fun(fun(fun(++count)));
    }
    return count;
}
 
public static void main(String[] args)
{
    fun(1);
}
}
 
// This code is contributed by Shubhamsingh10

Python3

def fun(count):
    print(count)
    if(count < 3):
        count+=1
        fun(fun(fun(count)))
     
    return count
  
 
if __name__=="__main__": 
     
    fun(1)
 
# This code is contributed by Shubhamsingh10

C#

using System;
 
class GFG{
     
    static int fun(int count) 
    { 
        Console.Write(count+"\n"); 
        if(count < 3) 
        { 
            fun(fun(fun(++count))); 
        } 
        return count; 
    } 
       
    static public void Main ()
    { 
        fun(1);  
    }
}
 
// This code is contributed by shubhamsingh10

Java脚本


输出:

1
 2
 3
 3
 3
 3
 3