📌  相关文章
📜  查找所有具有连续数字的 N 位整数

📅  最后修改于: 2022-05-13 01:56:09.750000             🧑  作者: Mango

查找所有具有连续数字的 N 位整数

给定一个整数N ,任务是找到所有具有连续数字的N 位整数

例子

方法:可以通过找到具有连续数字的第一个N位数字然后将( 111 ... N次)添加到它,直到最后一个数字小于等于( 999 ... N次)来解决该任务。请按照以下步骤解决问题:

  • 对于任何N第一个连续数字将是( 123 … N 次
  • 因此将长度为N第一个连续数字存储在num
  • 由于每个连续的数字将相差111 ... N 次),因此将此值存储在另一个变量add中。
  • 现在在循环的帮助下,只需在每次迭代中将 add 添加num即可找到所有长度为N的连续数字

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find all the N digit
// integers with sequential digits
vector sequentialDigits(int n)
{
    vector arr;
 
    // Stores the first N-digit
    // sequential number
    // (123 ... N times)
    long num = 0;
    for (int j = 0; j < n; j++)
        num = num * 10 + (j + 1);
 
    // Stores the difference
    // (111 ... N times)
    int add = 0;
    for (int i = 0; i < n; i++)
        add = add * 10 + 1;
 
    while (num % 10 > 0 && num % 10 <= 9
           && floor(log10(num) + 1) == n) {
 
        arr.push_back(num);
        num += add;
    }
 
    return arr;
}
 
// Driver Code
int main()
{
    int N = 4;
    vector ans = sequentialDigits(N);
 
    // Print the required numbers
    for (auto& it : ans)
        cout << it << ' ';
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
  // Function to find all the N digit
  // integers with sequential digits
  static Vector sequentialDigits(int n)
  {
    Vector arr = new Vector();
 
    // Stores the first N-digit
    // sequential number
    // (123 ... N times)
    long num = 0;
    for (int j = 0; j < n; j++)
      num = num * 10 + (j + 1);
 
    // Stores the difference
    // (111 ... N times)
    int add = 0;
    for (int i = 0; i < n; i++)
      add = add * 10 + 1;
 
    while (num % 10 > 0 && num % 10 <= 9
           && Math.floor(Math.log10(num) + 1) == n) {
 
      arr.add((int) num);
      num += add;
    }
 
    return arr;
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 4;
    Vector ans = sequentialDigits(N);
 
    // Print the required numbers
    for (int it : ans)
      System.out.print(it +" ");
  }
}
 
// This code is contributed by shikhasingrajput


Python3
# Python program for the above approach
import math
 
# Function to find all the N digit
# integers with sequential digits
def sequentialDigits(n):
 
    arr = []
 
    # Stores the first N-digit
    # sequential number
    # (123 ... N times)
    num = 0
    for j in range(0, n):
        num = num * 10 + (j + 1)
 
    # Stores the difference
    # (111 ... N times)
    add = 0
    for i in range(0, n):
        add = add * 10 + 1
 
    while (num % 10 > 0 and num % 10 <= 9 and math.floor(math.log10(num) + 1) == n):
 
        arr.append(num)
        num += add
 
    return arr
 
# Driver Code
N = 4
ans = sequentialDigits(N)
 
# Print the required numbers
for i in range(0, len(ans)):
    print(ans[i], end=" ")
 
# This code is contributed by Taranpreet


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG{
 
  // Function to find all the N digit
  // integers with sequential digits
  static List sequentialDigits(int n)
  {
    List arr = new List();
 
    // Stores the first N-digit
    // sequential number
    // (123 ... N times)
    long num = 0;
    for (int j = 0; j < n; j++)
      num = num * 10 + (j + 1);
 
    // Stores the difference
    // (111 ... N times)
    int add = 0;
    for (int i = 0; i < n; i++)
      add = add * 10 + 1;
 
    while (num % 10 > 0 && num % 10 <= 9
           && Math.Floor(Math.Log10(num) + 1) == n) {
 
      arr.Add((int) num);
      num += add;
    }
 
    return arr;
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    int N = 4;
    List ans = sequentialDigits(N);
 
    // Print the required numbers
    foreach (int it in ans)
      Console.Write(it +" ");
  }
}
 
// This code is contributed by shikhasingrajput


Javascript



输出
1234 2345 3456 4567 5678 6789 

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