📌  相关文章
📜  打印数字的所有子字符串,无需任何转换

📅  最后修改于: 2021-04-24 17:24:58             🧑  作者: Mango

给定整数N,任务是打印N的所有子字符串,而不进行任何转换,即将其转换为字符串或数组。

例子

方法:

  1. 根据大小取10的幂。
  2. 除以数字直至变为0并打印。
  3. 然后通过对k取模,将数字更改为该数字的下一个位置。
  4. 更新编号的数字。
  5. 重复相同的过程,直到n变为0。

下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
  
// Function to print the substrings of a number
void printSubstrings(int n)
{
    // Calculate the total number of digits
    int s = log10(n);
  
    // 0.5 has been added because of it will
    // return double value like 99.556
    int d = (int)(pow(10, s) + 0.5);
    int k = d;
  
    while (n) {
  
        // Print all the numbers from
        // starting position
        while (d) {
            cout << n / d << endl;
            d = d / 10;
        }
  
        // Update the no.
        n = n % k;
  
        // Update the no.of digits
        k = k / 10;
        d = k;
    }
}
  
// Driver code
int main()
{
    int n = 123;
    printSubstrings(n);
  
    return 0;
}


Java
// Java implementation 
// of above approach
import java.util.*;
import java.lang.*;
import java.io.*;
  
class GFG
{
// Function to print the
// substrings of a number
static void printSubstrings(int n)
{
    // Calculate the total
    // number of digits
    int s = (int)Math.log10(n);
  
    // 0.5 has been added because 
    // of it will return double
    // value like 99.556
    int d = (int)(Math.pow(10, s) + 0.5);
    int k = d;
  
    while (n > 0)
    {
  
        // Print all the numbers 
        // from starting position
        while (d > 0) 
        {
            System.out.println(n / d);
            d = d / 10;
        }
  
        // Update the no.
        n = n % k;
  
        // Update the no.of digits
        k = k / 10;
        d = k;
    }
}
  
// Driver code
public static void main(String args[])
{
    int n = 123;
    printSubstrings(n);
}
}
  
// This code is contributed 
// by Subhadeep


Python3
# Python3 implementation of above approach
import math
  
# Function to print the substrings of a number
def printSubstrings(n):
      
    # Calculate the total number of digits
    s = int(math.log10(n));
  
    # 0.5 has been added because of it will
    # return double value like 99.556
    d = (math.pow(10, s));
    k = d;
  
    while (n > 0):
  
        # Print all the numbers from
        # starting position
        while (d > 0):
            print(int(n // d));
            d = int(d / 10);
  
        # Update the no.
        n = int(n % k);
  
        # Update the no.of digits
        k = int(k // 10);
        d = k;
  
# Driver code
if __name__ == '__main__':
    n = 123;
    printSubstrings(n);
  
# This code is contributed by Rajput-Ji


C#
// C# implementation 
// of above approach
using System;
  
class GFG
{
// Function to print the
// substrings of a number
static void printSubstrings(int n)
{
    // Calculate the total
    // number of digits
    int s = (int)Math.Log10(n);
  
    // 0.5 has been added because 
    // of it will return double
    // value like 99.556
    int d = (int)(Math.Pow(10, s) + 0.5);
    int k = d;
  
    while (n > 0)
    {
  
        // Print all the numbers 
        // from starting position
        while (d > 0) 
        {
            Console.WriteLine(n / d);
            d = d / 10;
        }
  
        // Update the no.
        n = n % k;
  
        // Update the no.of digits
        k = k / 10;
        d = k;
    }
}
  
// Driver code
public static void Main()
{
    int n = 123;
    printSubstrings(n);
}
}
  
// This code is contributed 
// by mits


PHP


输出:
1
12
123
2
23
3