📜  生成一个以N和K的相邻差开始的双音数组

📅  最后修改于: 2021-05-04 19:10:55             🧑  作者: Mango

给定两个整数NK ,任务是生成一个双音阵列,其中第一个元素为N,每个元素的差为K。
例子:

方法:想法是使用递归来解决此问题。如问题中所述,双音阵列的第一个元素是N。因此,将其附加到数组中并求解N。以下是递归函数定义:

  • 基本情况:当N的值小于等于0时,返回1,因为现在值将增加。
  • 递归情况:如果N的值大于,则附加N-K并递归调用N-K,最后附加N。

下面是上述方法的实现:

C++
// C++ implementation to generate a
// Bitonic array where consecutive 
// elements are at difference of K
  
#include 
using namespace std;
  
// Recursive function to generate a
// Bitonic array where consecutive 
// elements are at the difference of K
int decreseq(int n, int k)
{
    // Recursively call until N > 0
    if (n > 0) {
          
        // Print decreasing sequence
        cout << n - k << " ";
        decreseq(n - k, k);
    }
      
    // if N less than 0 then 
    // particular function return 1
    if (n <= 0)
        return 1;
          
    // Print incresing sequence
    cout << n << " ";
    return 1;
}
  
// Driver Code
int main()
{
    int n = 10, k = 5;
    cout << n << " ";
    decreseq(n, k);
    return 0;
}


Java
// Java implementation to generate a
// Bitonic array where consecutive 
// elements are at difference of K
import java.util.*;
class GFG{
   
// Recursive function to generate a
// Bitonic array where consecutive 
// elements are at the difference of K
static int decreseq(int n, int k)
{
    // Recursively call until N > 0
    if (n > 0) 
    {
           
        // Print decreasing sequence
        System.out.print(n - k + " ");
        decreseq(n - k, k);
    }
       
    // if N less than 0 then 
    // particular function return 1
    if (n <= 0)
        return 1;
           
    // Print incresing sequence
    System.out.print(n + " ");
    return 1;
}
   
// Driver Code
public static void main(String[] args)
{
    int n = 10, k = 5;
    System.out.print(n+ " ");
    decreseq(n, k);
}
}
  
// This code is contributed by sapnasingh4991


Python3
# Python3 implementation to generate a
# Bitonic array where consecutive 
# elements are at difference of K
  
# Recursive function to generate a
# Bitonic array where consecutive 
# elements are at the difference of K
def decreseq(n, k):
  
    # Recursively call until N > 0
    if (n > 0):
          
        # Print decreasing sequence
        print(n - k, end = " ");
        decreseq(n - k, k);
      
    # if N less than 0 then 
    # particular function return 1
    if (n <= 0):
        return 1;
          
    # Print incresing sequence
    print(n, end = " ");
    return 1;
  
# Driver Code
n = 10; k = 5;
print(n, end = " ");
decreseq(n, k);
  
# This code is contributed by Code_Mech


C#
// C# implementation to generate a
// Bitonic array where consecutive 
// elements are at difference of K
using System;
  
class GFG{
  
// Recursive function to generate a
// Bitonic array where consecutive 
// elements are at the difference of K
static int decreseq(int n, int k)
{
      
    // Recursively call until N > 0
    if (n > 0) 
    {
          
        // Print decreasing sequence
        Console.Write(n - k + " ");
        decreseq(n - k, k);
    }
      
    // If N less than 0 then 
    // particular function return 1
    if (n <= 0)
        return 1;
          
    // Print incresing sequence
    Console.Write(n + " ");
    return 1;
}
  
// Driver Code
public static void Main(String[] args)
{
    int n = 10, k = 5;
      
    Console.Write(n + " ");
      
    decreseq(n, k);
}
}
  
// This code is contributed by gauravrajput1


输出:
10 5 0 5 10