给定两个整数N和K ,任务是生成所有长度为N且相邻数字的绝对差等于K的正整数。
例子:
Input: N = 4, K = 8
Output: 1919, 8080, 9191
Explanation:
The absolute difference between every consecutive digit of each number is 8.
For Example: 8080 (abs(8-0) = 8, abs(0-8) = 8)
Input: N = 2, K = 2
Output: 13, 24, 20, 35, 31, 46, 42, 57, 53, 68, 64, 79, 75, 86, 97
Explanation:
The absolute difference between every consecutive digit of each number is 1.
方法:想法是使用回溯。遍历位[1,9]并为每个数字形式具有如使用递归ķ绝对数字的差的N位数字。步骤如下:
- 创建一个向量ans []来存储所有结果数字,然后从1到9递归迭代以生成从每个数字开始的数字。情况如下:
- 基本情况:对于单个长度的所有整数,即N = 1 ,将它们添加到ans []中。
- 递归调用:如果将数字K加到某位数字上不超过9 ,则通过减小N递归调用并将num更新为(10 * num + num%10 + K) ,如下所示:
if(num % 10 + K <= 9) {
recursive_function(10 * num + (num % 10 + K), K, N – 1, and);
} - 如果在所有递归调用之后K的值都不为零,并且num%10> = K ,则再次递归调用,方法是减小N并将num更新为(10 * num + num%10 – K),如下所示:
recursive_function(10 * num + num % 10 – K, K, N – 1, ans);
- 完成上述所有步骤后,打印存储在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
输出:
1919, 8080, 9191,
时间复杂度: O(2 N )
辅助空间: O(N)