原文:
给定一个整数 n,任务是打印 n 的所有子字符串,而不做任何转换,即将其转换为字符串或数组。
示例:
输入 : n = 12345 输出:可能的子串:{1,12,123,1234,12345,2,23,234,2345,3,34,345,4,45,5} 输入 : n = 123 输出:可能的子串:{1,12,123,2,23,3
进场:
- 根据大小取 10 的幂。
- 将数字除以 0,然后打印出来。
- 然后通过取 k 的模,将该数改变到该数的下一个位置。
- 更新位数。
- 重复同样的过程,直到 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
python 3
# 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
服务器端编程语言(professional hypertext preprocessor 的缩写)
java 描述语言
output:
1
12
123
2
23
3
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处