原文:

问题–给定两个数字 n 和 k,我们的任务是从 n 中减去一个数字 k,直到数字(n)大于零,一旦 n 变成负或零,我们就开始加 k,直到那个数字变成原来的数字(n)。 注意:不允许使用任何循环。 例:

input : n = 15 k = 5  
output : 15 10 5 0 1 5 10 15
input : n = 20 k = 6
output : 20 14 8 2 -4 2 8 14 20 

解释–我们可以用递归的思想来做,就是我们一次又一次的调用函数,直到 n 大于零(在每次的函数调用中,我们用 k 减去 n)。一旦数字变成负数或零,我们开始在每个函数调用中添加 k,直到数字变成原始数字。这里我们使用一个函数进行加法和减法,但是为了在加法和减法之间切换,我们使用了一个布尔标志

c

// c   program to print number
// series without using loop
#include 
using namespace std;
// function print series
// using recursion
void printnumber(int n, int original, int k, bool flag)
{
    // print the number
    cout << n << " ";
    // change flag if number
    // become negative
    if (n <= 0)
        flag = !flag;
    // base condition for
    // second_case (adding k)
    if (n == original && !flag)
        return;
    // if flag is true
    // we subtract value until
    // number is greater then zero
    if (flag == true) {
        printnumber(n - k, original, k, flag);
        return;
    }
    // second case (addition )
    if (!flag) {
        printnumber(n   k, original, k, flag);
        return;
    }
}
// driver program
int main()
{
    int n = 20, k = 6;
    printnumber(n, n, k, true);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to print number
// series without using loop
import java.io.*;
import java.util.*;
class gfg
{
    public static void printnumber(int n, int original, int k, boolean flag)
    {
        // print the number
        system.out.print(n   " ");
        // change flag if number
        // become negative
        if (n <= 0)
            flag = !flag;
        // base condition for
        // second_case (adding k)
        if (n == original && !flag)
            return;
        // if flag is true
        // we subtract value until
        // number is greater then zero
        if (flag == true)
        {
            printnumber(n - k, original, k, flag);
            return;
        }
        // second case (addition )
        if (!flag)
        {
            printnumber(n   k, original, k, flag);
            return;
        }
    }
    public static void main (string[] args)
    {
        int n = 20, k = 6;
        printnumber(n, n, k, true);
    }
}
// this code is contributed by mohit gupta_omg

python 3

# python program to print number
# series without using loop
def printnumber(n, original, k, flag):
    #print the number
    print(n, end = " ")
    # change flag if number
    # become negative
    if (n <= 0):
        if(flag==0):
            flag = 1
        else:
            flag = 0
    # base condition for
    # second_case (adding k)
    if (n == original and (not(flag))):
        return
    # if flag is true
    # we subtract value until
    # number is greater then zero
    if (flag == true):
        printnumber(n - k, original, k, flag)
        return
    # second case (addition )
    if (not(flag)):
        printnumber(n   k, original, k, flag);
        return
n = 20
k = 6
printnumber(n, n, k, true)
# this code is contributed by mohit gupta_omg

c

// c# program to print number
// series without using loop
using system;
public class gfg {
    // function print series
    // using recursion
    static void printnumber(int n,
             int original, int k, bool flag)
    {
        // print the number
        console.write(n   " ");
        // change flag if number
        // become negative
        if (n <= 0)
            flag = !flag;
        // base condition for
        // second_case (adding k)
        if (n == original && !flag)
            return;
        // if flag is true
        // we subtract value until
        // number is greater then zero
        if (flag == true)
        {
            printnumber(n - k, original, k,
                                      flag);
            return;
        }
        // second case (addition )
        if (!flag)
        {
            printnumber(n   k, original, k,
                                      flag);
            return;
        }
    }
    // driver program
    static public void main ()
    {
        int n = 20, k = 6;
        printnumber(n, n, k, true);
    }
}
// this code is contributed by vt_m.

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


输出:

20 14 8 2 -4 2 8 14 20