📌  相关文章
📜  生成相邻数字之间的绝对差为K的所有N个数字

📅  最后修改于: 2021-05-24 19:41:58             🧑  作者: Mango

给定两个整数NK ,任务是生成所有长度为N且相邻数字的绝对差等于K的正整数。

例子:

方法:想法是使用回溯。使用递归对数字[1,9]进行迭代并从具有绝对数字差异的N位数字开始,对每个数字进行K迭代。步骤如下:

1.创建一个向量ans []来存储所有结果数字,然后从1到9递归迭代以生成从每个数字开始的数字。情况如下:

  • 基本情况:对于单个长度的所有整数,即N = 1 ,将它们添加到ans []中
  • 递归调用:如果将数字K加到某位数字上不超过9 ,则通过减小N递归调用并将num更新为(10 * num + num%10 + K) ,如下所示:
  • 如果在所有递归调用之后K的值都不为零,并且num%10> = K ,则再次递归调用,方法是减小N并将num更新为(10 * num + num%10 – K),如下所示:

2.完成上述所有步骤后,打印存储在ans []中的数字。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function that recursively finds the
// possible numbers and append into ans
void checkUntil(int num, int K,
                int N, vector& ans)
{
   
    // Base Case
    if (N == 1)
    {
        ans.push_back(num);
        return;
    }
 
    // Check the sum of last digit and k
    // less than or equal to 9 or not
    if ((num % 10 + K) <= 9)
        checkUntil(10 * num
                       + (num % 10 + K),
                   K, N - 1, ans);
 
    // If k==0, then subtraction and
    // addition does not make any
    // difference
    // Hence, subtraction is skipped
    if (K) {
        if ((num % 10 - K) >= 0)
            checkUntil(10 * num
                           + num % 10 - K,
                       K, N - 1,
                       ans);
    }
}
 
// Function to call checkUntil function
// for every integer from 1 to 9
void check(int K, int N, vector& ans)
{
    // check_util function recursively
    // store all numbers starting from i
    for (int i = 1; i <= 9; i++) {
        checkUntil(i, K, N, ans);
    }
}
 
// Function to print the all numbers
// which  satisfy the conditions
void print(vector& ans)
{
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << ", ";
    }
}
 
// Driver Code
int main()
{
    // Given N and K
    int N = 4, K = 8;
 
    // To store the result
    vector ans;
 
    // Function Call
    check(K, N, ans);
 
    // Print Resultant Numbers
    print(ans);
    return 0;
}


Java
// Java program for
// the above approach
import java.util.*;
class GFG{
 
    // Function that recursively finds the
    // possible numbers and append into ans
    static void checkUntil(int num, int K, int N,
                           Vector ans)
    {
       
        // Base Case
        if (N == 1)
        {
            ans.add(num);
            return;
        }
 
        // Check the sum of last digit and k
        // less than or equal to 9 or not
        if ((num % 10 + K) <= 9)
            checkUntil(10 * num + (num % 10 + K),
                       K, N - 1, ans);
 
        // If k==0, then subtraction and
        // addition does not make any
        // difference
        // Hence, subtraction is skipped
        if (K > 0)
        {
            if ((num % 10 - K) >= 0)
                checkUntil(10 * num + num % 10 - K,
                           K, N - 1, ans);
        }
    }
 
    // Function to call checkUntil function
    // for every integer from 1 to 9
    static void check(int K, int N, Vector ans)
    {
       
        // check_util function recursively
        // store all numbers starting from i
        for (int i = 1; i <= 9; i++)
        {
            checkUntil(i, K, N, ans);
        }
    }
 
    // Function to print the all numbers
    // which  satisfy the conditions
    static void print(Vector ans)
    {
        for (int i = 0; i < ans.size(); i++)
        {
            System.out.print(ans.get(i) + ", ");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given N and K
        int N = 4, K = 8;
 
        // To store the result
        Vector ans = new Vector();;
 
        // Function Call
        check(K, N, ans);
 
        // Print Resultant Numbers
        print(ans);
    }
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function that recursively finds the
# possible numbers and append into ans
def checkUntil(num, K, N, ans):
     
    # Base Case
    if (N == 1):
        ans.append(num)
        return
 
    # Check the sum of last digit and k
    # less than or equal to 9 or not
    if ((num % 10 + K) <= 9):
        checkUntil(10 * num +
                 (num % 10 + K),
                 K, N - 1, ans)
 
    # If k==0, then subtraction and
    # addition does not make any
    # difference
    # Hence, subtraction is skipped
    if (K):
        if ((num % 10 - K) >= 0):
            checkUntil(10 * num +
                      num % 10 - K,
                     K, N - 1, ans)
     
# Function to call checkUntil function
# for every integer from 1 to 9
def check(K, N, ans):
     
    # check_util function recursively
    # store all numbers starting from i
    for i in range(1, 10):
        checkUntil(i, K, N, ans)
 
# Function to print the all numbers
# which satisfy the conditions
def print_list(ans):
     
    for i in range(len(ans)):
        print(ans[i], end = ", ")
 
# Driver Code
if __name__ == "__main__":
 
    # Given N and K
    N = 4
    K = 8;
 
    # To store the result
    ans = []
 
    # Function call
    check(K, N, ans)
 
    # Print resultant numbers
    print_list(ans)
 
# This code is contributed by chitranayal


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function that recursively finds the
// possible numbers and append into ans
static void checkUntil(int num, int K, int N,
                       List ans)
{
 
    // Base Case
    if (N == 1)
    {
        ans.Add(num);
        return;
    }
 
    // Check the sum of last digit and k
    // less than or equal to 9 or not
    if ((num % 10 + K) <= 9)
        checkUntil(10 * num + (num % 10 + K),
                 K, N - 1, ans);
 
    // If k==0, then subtraction and
    // addition does not make any
    // difference
    // Hence, subtraction is skipped
    if (K > 0)
    {
        if ((num % 10 - K) >= 0)
            checkUntil(10 * num + num % 10 - K,
                     K, N - 1, ans);
    }
}
 
// Function to call checkUntil function
// for every integer from 1 to 9
static void check(int K, int N, List ans)
{
 
    // check_util function recursively
    // store all numbers starting from i
    for(int i = 1; i <= 9; i++)
    {
        checkUntil(i, K, N, ans);
    }
}
 
// Function to print the all numbers
// which satisfy the conditions
static void print(List ans)
{
    for(int i = 0; i < ans.Count; i++)
    {
        Console.Write(ans[i] + ", ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given N and K
    int N = 4, K = 8;
 
    // To store the result
    List ans = new List();;
 
    // Function call
    check(K, N, ans);
 
    // Print Resultant Numbers
    print(ans);
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
1919, 8080, 9191,

时间复杂度: O(2 N )
辅助空间: O(N)