📜  使用间接递归打印数字1到N

📅  最后修改于: 2021-04-29 04:17:11             🧑  作者: Mango

给定数字N,我们需要打印从1到N的数字,而没有直接递归,循环,标签。基本上,我们需要在上面的代码段中插入代码,以便可以打印从1到N的数字?

#include 
#define N 20;
int main()
{ 
   // Your code goes Here.
}


例子 :

Input  : 10
Output : 1 2 3 4 5 6 7 8 9 10

Input  : 5
Output : 1 2 3 4 5

我们已经在以下帖子中讨论了解决方案:
在C++中打印1到100,没有循环和递归
在不使用循环的情况下,如何打印从1到100的数字?

这是可以打印1到100之间的数字而无需直接递归,循环和标签的代码。该代码使用间接递归。

C
// C program to print from 1 to N using
// indirect recursion/
#include
  
// We can avoid use of these using references
#define N 20;
int n = 1;
  
// Prints n, increments n and calls fun1()
void fun1()
{
    if (n <= N)
    {
        printf("%d", n);
        n++;
        fun2();
    }
    else
        return;
}
  
// Prints n, increments n and calls fun2()
void fun2()
{
    if (n <= N)
    {
        printf("%d", n);
        n++;
        fun1();
    }
    else
        return;
}
  
// Driver Program
int main(void)
{
    fun1();
    return 0;
}


Java
// Java program to print from 1 to N using
// indirect recursion
class GFG 
{
    // We can avoid use of these using references
    static final int N = 20;
    static int n = 1;
  
    // Prints n, increments n and calls fun1()
    static void fun1() 
    {
        if (n <= N) 
        {
            System.out.printf("%d ", n);
            n++;
            fun2();
        }
        else
        {
            return;
        }
    }
  
    // Prints n, increments n and calls fun2()
    static void fun2() 
    {
        if (n <= N)
        {
            System.out.printf("%d ", n);
            n++;
            fun1();
        } 
        else 
        {
            return;
        }
    }
  
    // Driver Program
    public static void main(String[] args) 
    {
        fun1();
    }
}
  
// This code is contributed by Rajput-Ji


Python3
# Python program to prfrom 1 to N using
# indirect recursion
  
# We can avoid use of these using references
N = 20;
n = 1;
  
# Prints n, increments n and calls fun1()
def fun1():
    global N, n;
    if (n <= N):
        print(n, end = " ");
        n += 1;
        fun2();
    else:
        return;
  
# Prints n, increments n and calls fun2()
def fun2():
    global N, n;
    if (n <= N):
        print(n, end = " ");
        n += 1;
        fun1();
    else:
        return;
  
# Driver Program
if __name__ == '__main__':
  
    fun1();
  
# This code is contributed by 29AjayKumar


C#
// C# program to print from 1 to N using
// indirect recursion
using System;
  
class GFG 
{
    // We can avoid use of these using references
    static readonly int N = 20;
    static int n = 1;
  
    // Prints n, increments n and calls fun1()
    static void fun1() 
    {
        if (n <= N) 
        {
            Console.Write("{0} ", n);
            n++;
            fun2();
        }
        else
        {
            return;
        }
    }
  
    // Prints n, increments n and calls fun2()
    static void fun2() 
    {
        if (n <= N)
        {
            Console.Write("{0} ", n);
            n++;
            fun1();
        } 
        else
        {
            return;
        }
    }
  
    // Driver Code
    public static void Main(String[] args) 
    {
        fun1();
    }
}
  
// This code is contributed by Rajput-Ji


PHP


输出 :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 

这是如何工作的:
在上述程序中,我们仅使用了两个功能。一个调用其他,另一个调用前一个,因此是间接递归。

锻炼 :
修改上面的程序以使用N作为参数,而不是使其成为全局参数。