相邻数字之间具有绝对差异的N位数字的计数为K
给定两个整数N和K。任务是计算所有长度为 N且相邻数字之间的绝对差等于K的正整数。
例子:
Input: N = 4, K = 8
Output: 3
Explanation: The absolute difference between every consecutive digit of each number is 8. Three possible numbers are 8080, 1919 and 9191.
Input: N = 2, K = 0
Output: 9
Explanation: 11, 22, 33, 44, 55, 66, 77, 88, 99. The absolute difference between every consecutive digit of each number is 0.
方法:该方法基于递归。迭代数字[1, 9],对于每个数字,使用递归将具有绝对数字差异的N位数字计数为K。以下情况到达递归函数调用。
- 基本情况:对于所有个位数整数,即 N = 1,增加答案计数。
- 递归调用:如果到现在形成的数字(num)的个位数加上数字K不超过9,则通过减少N并使得num = (num*10 + num%10 + K)递归调用。
if(num % 10 + K ≤ 9)
recursive_function(10 * num + (num % 10 + K), N – 1);
- 如果在所有递归调用之后K的值非零并且如果num % 10 ≥ K ,则通过减小N并将num更新为(10*num + num%10 – K) 再次递归调用。
if(num % 10 ≥ K)
recursive_function(10 * num + num % 10 – K, N – 1)
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// To store the count of numbers
int countNums = 0;
// Function that recursively finds the
// possible numbers and append into ans
void checkUtil(int num, int K, int N)
{
// Base Case
if (N == 1) {
countNums++;
return;
}
// Check the sum of last digit and k
// less than or equal to 9 or not
if ((num % 10 + K) <= 9)
checkUtil(10 * num +
(num % 10 + K), K, N - 1);
// If K = 0, then subtraction and
// addition does not make any
// difference
if (K) {
// If unit's digit greater than K
if ((num % 10 - K) >= 0)
checkUtil(10 * num +
num % 10 - K, K, N - 1);
}
}
// Function to call checkUtil function
// for every integer from 1 to 9
void check(int K, int N)
{
// Loop to check for
// all digits from 1 to 9
for (int i = 1; i <= 9; i++) {
checkUtil(i, K, N);
}
}
// Driver Code
int main()
{
// Given N and K
int N = 4, K = 8;
check(K, N);
// Count total possible numbers
cout << countNums << endl;
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// To store the count of numbers
static int countNums = 0;
// Function that recursively finds the
// possible numbers and append into ans
static void checkUtil(int num, int K, int N)
{
// Base Case
if (N == 1) {
countNums++;
return;
}
// Check the sum of last digit and k
// less than or equal to 9 or not
if ((num % 10 + K) <= 9)
checkUtil(10 * num +
(num % 10 + K), K, N - 1);
// If K = 0, then subtraction and
// addition does not make any
// difference
if (K>0) {
// If unit's digit greater than K
if ((num % 10 - K) >= 0)
checkUtil(10 * num +
num % 10 - K, K, N - 1);
}
}
// Function to call checkUtil function
// for every integer from 1 to 9
static void check(int K, int N)
{
// Loop to check for
// all digits from 1 to 9
for (int i = 1; i <= 9; i++) {
checkUtil(i, K, N);
}
}
// Driver Code
public static void main(String[] args)
{
// Given N and K
int N = 4, K = 8;
check(K, N);
// Count total possible numbers
System.out.print(countNums +"\n");
}
}
// This code contributed by shikhasingrajput
Python3
# Python program for the above approach
# To store the count of numbers
countNums = 0;
# Function that recursively finds the
# possible numbers and append into ans
def checkUtil(num, K, N):
global countNums;
# Base Case
if (N == 1):
countNums += 1;
return;
# Check the sum of last digit and k
# less than or equal to 9 or not
if ((num % 10 + K) <= 9):
checkUtil(10 * num + (num % 10 + K), K, N - 1);
# If K = 0, then subtraction and
# addition does not make any
# difference
if (K > 0):
# If unit's digit greater than K
if ((num % 10 - K) >= 0):
checkUtil(10 * num + num % 10 - K, K, N - 1);
# Function to call checkUtil function
# for every integer from 1 to 9
def check(K, N):
# Loop to check for
# all digits from 1 to 9
for i in range(1,10):
checkUtil(i, K, N);
# Driver Code
if __name__ == '__main__':
# Given N and K
N = 4;
K = 8;
check(K, N);
# Count total possible numbers
print(countNums);
# This code is contributed by shikhasingrajput
C#
// C# program for the above approach
using System;
class GFG{
// To store the count of numbers
static int countNums = 0;
// Function that recursively finds the
// possible numbers and append into ans
static void checkUtil(int num, int K, int N)
{
// Base Case
if (N == 1)
{
countNums++;
return;
}
// Check the sum of last digit and k
// less than or equal to 9 or not
if ((num % 10 + K) <= 9)
checkUtil(10 * num +
(num % 10 + K), K, N - 1);
// If K = 0, then subtraction and
// addition does not make any
// difference
if (K > 0)
{
// If unit's digit greater than K
if ((num % 10 - K) >= 0)
checkUtil(10 * num +
num % 10 - K, K, N - 1);
}
}
// Function to call checkUtil function
// for every integer from 1 to 9
static void check(int K, int N)
{
// Loop to check for
// all digits from 1 to 9
for(int i = 1; i <= 9; i++)
{
checkUtil(i, K, N);
}
}
// Driver Code
public static void Main(String[] args)
{
// Given N and K
int N = 4, K = 8;
check(K, N);
// Count total possible numbers
Console.Write(countNums + "\n");
}
}
// This code is contributed by 29AjayKumar
Javascript
输出
3
时间复杂度: O(2 N )
辅助空间: O(1)