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

📅  最后修改于: 2021-09-06 11:32:34             🧑  作者: 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


Javascript


输出:
10 5 0 5 10

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live