原文:

给定一个自然数 n ,任务是打印第 n 个。

如果所有相邻数字的绝对差值都为 1,则该数字称为步进数。以下系列为步进自然数列表: 1、2、3、4、5、6、7、8、9、10、11、12、21、22、23、32、…。

例:

input: n = 16 
output: 32
explanation:
16th stepping number is 32.
input: n = 14 
output: 22
explanation:
14th stepping number is 22.

方法:这个问题可以使用 数据结构来解决。首先,准备一个空队列,按照的顺序将 1,2,…,9 入队。 然后为了生成第 n 个步进号,必须执行以下操作 n 次:

  • 从队列中执行出列。设 x 为出列元素。
  • 如果 x mod 10 不等于 0,则入队 10x (x mod 10)–1
  • enqueue 10x (x mod 10)。
  • 如果 x mod 10 不等于 9,那么入队 10x (x mod 10) 1。

第 n 个操作中的出列号是第 n 个步进号。 以下是上述方法的实施:

c

// c   implementation to find
// n’th stepping natural number
#include 
using namespace std;
// function to find the
// nth stepping natural number
int nthsmallest(int k)
{
    // declare the queue
    queue q;
    int x;
    // enqueue 1, 2, ..., 9 in this order
    for (int i = 1; i < 10; i  )
        q.push(i);
    // perform k operation on queue
    for (int i = 1; i <= k; i  ) {
        // get the ith stepping number
        x = q.front();
        // perform dequeue from the queue
        q.pop();
        // if x mod 10 is not equal to 0
        if (x % 10 != 0) {
            // then enqueue 10x   (x mod 10) - 1
            q.push(x * 10   x % 10 - 1);
        }
        // enqueue 10x   (x mod 10)
        q.push(x * 10   x % 10);
        // if x mod 10 is not equal to 9
        if (x % 10 != 9) {
            // then enqueue 10x   (x mod 10)   1
            q.push(x * 10   x % 10   1);
        }
    }
    // return the dequeued number of the k-th
    // operation as the nth stepping number
    return x;
}
// driver code
int main()
{
    // initialise k
    int n = 16;
    cout << nthsmallest(n) << "\n";
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java implementation to find
// n'th stepping natural number
import java.util.*;
class gfg{
// function to find the
// nth stepping natural number
static int nthsmallest(int k)
{
    // declare the queue
    queue q = new linkedlist<>();
    int x = 0;
    // enqueue 1, 2, ..., 9 in this order
    for (int i = 1; i < 10; i  )
        q.add(i);
    // perform k operation on queue
    for (int i = 1; i <= k; i  ) {
        // get the ith stepping number
        x = q.peek();
        // perform dequeue from the queue
        q.remove();
        // if x mod 10 is not equal to 0
        if (x % 10 != 0) {
            // then enqueue 10x   (x mod 10) - 1
            q.add(x * 10   x % 10 - 1);
        }
        // enqueue 10x   (x mod 10)
        q.add(x * 10   x % 10);
        // if x mod 10 is not equal to 9
        if (x % 10 != 9) {
            // then enqueue 10x   (x mod 10)   1
            q.add(x * 10   x % 10   1);
        }
    }
    // return the dequeued number of the k-th
    // operation as the nth stepping number
    return x;
}
// driver code
public static void main(string[] args)
{
    // initialise k
    int n = 16;
    system.out.print(nthsmallest(n));
}
}
// this code is contributed by 29ajaykumar

python 3

# python3 implementation to find
# n’th stepping natural number
# function to find the
# nth stepping natural number
def nthsmallest(k):
    # declare the queue
    q = []
    # enqueue 1, 2, ..., 9 in this order
    for i in range(1,10):
        q.append(i)
    # perform k operation on queue
    for i in range(1,k 1):
        # get the ith stepping number
        x = q[0]
        # perform dequeue from the queue
        q.remove(q[0])
        # if x mod 10 is not equal to 0
        if (x % 10 != 0):
            # then enqueue 10x   (x mod 10) - 1
            q.append(x * 10   x % 10 - 1)
        # enqueue 10x   (x mod 10)
        q.append(x * 10   x % 10)
        # if x mod 10 is not equal to 9
        if (x % 10 != 9):
            # then enqueue 10x   (x mod 10)   1
            q.append(x * 10   x % 10   1)
    # return the dequeued number of the k-th
    # operation as the nth stepping number
    return x
# driver code
if __name__ == '__main__':
    # initialise k
    n = 16
    print(nthsmallest(n))
# this code is contributed by surendra_gangwar

c

// c# implementation to find
// n'th stepping natural number
using system;
using system.collections.generic;
class gfg{
// function to find the
// nth stepping natural number
static int nthsmallest(int k)
{
    // declare the queue
    list q = new list();
    int x = 0;
    // enqueue 1, 2, ..., 9 in this order
    for (int i = 1; i < 10; i  )
        q.add(i);
    // perform k operation on queue
    for (int i = 1; i <= k; i  ) {
        // get the ith stepping number
        x = q[0];
        // perform dequeue from the queue
        q.removeat(0);
        // if x mod 10 is not equal to 0
        if (x % 10 != 0) {
            // then enqueue 10x   (x mod 10) - 1
            q.add(x * 10   x % 10 - 1);
        }
        // enqueue 10x   (x mod 10)
        q.add(x * 10   x % 10);
        // if x mod 10 is not equal to 9
        if (x % 10 != 9) {
            // then enqueue 10x   (x mod 10)   1
            q.add(x * 10   x % 10   1);
        }
    }
    // return the dequeued number of the k-th
    // operation as the nth stepping number
    return x;
}
// driver code
public static void main(string[] args)
{
    // initialise k
    int n = 16;
    console.write(nthsmallest(n));
}
}
// this code is contributed by sapnasingh4991

java 描述语言


output: 

32

时间复杂度: o(n)

辅助空间:o(n)t4】