在通过从自然数中删除数字 K 形成的序列中找到第 N 项
给定整数N、K和一个无限的自然数序列,其中所有包含数字K (1<=K<=9) 的数字都被删除。任务是返回此序列的第 N 个数字。
例子:
Input: N = 12, K = 2
Output: 14
Explanation: The sequence generated for the above input would be like this: 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, up to infinity
Input: N = 10, K = 1
Output: 22
朴素方法:解决上述问题的基本方法是迭代到 N 并排除所有小于 N 的包含给定数字 K 的数字。最后,打印获得的第 N 个自然数。
时间复杂度:O(N)
辅助空间:O(1)
有效的方法:解决这个问题的有效方法是从第 N 个自然数中得到的灵感,在删除所有由数字 9 组成的数字之后。
如果 K 的值大于 8,则可以通过将K的值转换为以 9 为底的形式来解决给定的问题。可以遵循以下步骤:
- 将第 N 个自然数计算为以 9 为底的格式
- 以 9 为基数的大于等于 K 的每一位都加 1
- 下一个数字是想要的答案
以下是上述方法的代码:
C++
// C++ implementation for the above approach
#include
using namespace std;
long long convertToBase9(long long n)
{
long long ans = 0;
// Denotes the digit place
long long a = 1;
// Method to convert any number
// to binary equivalent
while (n > 0) {
ans += (a * (n % 9));
a *= 10;
n /= 9;
}
return ans;
}
long long getNthnumber(long long base9,
long long K)
{
long long ans = 0;
// denotes the current digits place
long long a = 1;
while (base9 > 0) {
int cur = base9 % 10;
// If current digit is >= K
// increment its value by 1
if (cur >= K) {
ans += a * (cur + 1);
}
// Else add the digit as it is
else {
ans += a * cur;
}
base9 /= 10;
// Move to the next digit
a *= 10;
}
return ans;
}
// Driver code
int main()
{
long long N = 10, K = 1;
long long base9 = convertToBase9(N);
cout << getNthnumber(base9, K);
return 0;
}
Java
// Java implementation for the above approach
import java.io.*;
class GFG {
static long convertToBase9(long n)
{
long ans = 0;
// Denotes the digit place
long a = 1;
// Method to convert any number
// to binary equivalent
while (n > 0) {
ans += (a * (n % 9));
a *= 10;
n /= 9;
}
return ans;
}
static long getNthnumber(long base9,
long K)
{
long ans = 0;
// denotes the current digits place
long a = 1;
while (base9 > 0) {
int cur = (int)(base9 % 10);
// If current digit is >= K
// increment its value by 1
if (cur >= K) {
ans += a * (cur + 1);
}
// Else add the digit as it is
else {
ans += a * cur;
}
base9 /= 10;
// Move to the next digit
a *= 10;
}
return ans;
}
// Driver code
public static void main (String[] args) {
long N = 10, K = 1;
long base9 = convertToBase9(N);
System.out.println(getNthnumber(base9, K));
}
}
// This code is contributed by Dharanendra L V.
Python3
# Python 3 implementation for the above approach
def convertToBase9(n):
ans = 0
# Denotes the digit place
a = 1
# Method to convert any number
# to binary equivalent
while(n > 0):
ans += (a * (n % 9))
a *= 10
n //= 9
return ans
def getNthnumber(base9, K):
ans = 0
# denotes the current digits place
a = 1
while (base9 > 0):
cur = base9 % 10
# If current digit is >= K
# increment its value by 1
if (cur >= K):
ans += a * (cur + 1)
# Else add the digit as it is
else:
ans += a * cur
base9 //= 10
# Move to the next digit
a *= 10
return ans
# Driver code
if __name__ == '__main__':
N = 10
K = 1
base9 = convertToBase9(N)
print(getNthnumber(base9, K))
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# implementation for the above approach
using System;
class GFG {
static long convertToBase9(long n)
{
long ans = 0;
// Denotes the digit place
long a = 1;
// Method to convert any number
// to binary equivalent
while (n > 0) {
ans += (a * (n % 9));
a *= 10;
n /= 9;
}
return ans;
}
static long getNthnumber(long base9,
long K)
{
long ans = 0;
// denotes the current digits place
long a = 1;
while (base9 > 0) {
int cur = (int)(base9 % 10);
// If current digit is >= K
// increment its value by 1
if (cur >= K) {
ans += a * (cur + 1);
}
// Else add the digit as it is
else {
ans += a * cur;
}
base9 /= 10;
// Move to the next digit
a *= 10;
}
return ans;
}
// Driver code
public static void Main (String[] args) {
long N = 10, K = 1;
long base9 = convertToBase9(N);
Console.Write(getNthnumber(base9, K));
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出:
22
时间复杂度: O(log 9 N)
辅助空间: O(1)