打印给定范围内由连续数字组成的所有数字
给定一个范围[L, R] ,任务是从范围[L, R]中找出所有数字连续的数字。按升序打印所有这些数字。
例子:
Input: L = 12, R = 34
Output: 12 23 34
Input: L = 12, R = 25
Output: 12 23
方法:给定问题可以通过生成所有可能的数字并存储所有满足给定条件的数字来解决。生成所有数字后,按排序顺序打印所有存储的数字。请按照以下步骤解决给定的问题:
- 初始化一个变量,比如num为“” ,它存储所有可能的数字的字符串形式,这些数字具有连续的数字并按升序排列。
- 使用变量i遍历范围 [1, 9] 并执行以下步骤:
- 将字符串num更新为i的字符串形式,如果该值位于[L, R]范围内,则将其存储在向量Ans[]中。
- 使用变量j在范围 [1, 9] 上进行迭代,将j的字符形式添加到字符串num中,如果字符串num的整数形式位于范围[L, R]中,则将其存储在向量Ans中[] 。
- 完成以上步骤后,对向量Ans[]进行排序,打印出所有生成的数字。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the consecutive
// digit numbers in the given range
vector solve(int start, int end)
{
// Initialize num as empty string
string num = "";
// Stores the resultant number
vector ans;
// Iterate over the range [1, 9]
for (int i = 1; i <= 9; i++) {
num = to_string(i);
int value = stoi(num);
// Check if the current number
// is within range
if (value >= start
and value <= end) {
ans.push_back(value);
}
// Iterate on the digits starting
// from i
for (int j = i + 1; j <= 9; j++) {
num += to_string(j);
value = stoi(num);
// Checking the consecutive
// digits numbers starting
// from i and ending at j
// is within range or not
if (value >= start
and value <= end) {
ans.push_back(value);
}
}
}
// Sort the numbers in the
// increasing order
sort(ans.begin(), ans.end());
return ans;
}
// Driver Code
int main()
{
int L = 12, R = 87;
vector ans = solve(12, 87);
// 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 the consecutive
// digit numbers in the given range
static Vector solve(int start, int end)
{
// Initialize num as empty String
String num = "";
// Stores the resultant number
Vector ans = new Vector<>();
// Iterate over the range [1, 9]
for(int i = 1; i <= 9; i++)
{
num = Integer.toString(i);
int value = i;
// Check if the current number
// is within range
if (value >= start &&
value <= end)
{
ans.add(value);
}
// Iterate on the digits starting
// from i
for(int j = i + 1; j <= 9; j++)
{
num += Integer.toString(j);
value = Integer.valueOf(num);
// Checking the consecutive
// digits numbers starting
// from i and ending at j
// is within range or not
if (value >= start &&
value <= end)
{
ans.add(value);
}
}
}
// Sort the numbers in the
// increasing order
Collections.sort(ans);;
return ans;
}
// Driver Code
public static void main(String[] args)
{
int L = 12, R = 87;
Vector ans = solve(L,R);
// Print the required numbers
for(int it : ans)
System.out.print(it + " ");
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 program for the above approach
# Function to find the consecutive
# digit numbers in the given range
def solve(start, end):
# Initialize num as empty string
num = ""
# Stores the resultant number
ans = []
# Iterate over the range [1, 9]
for i in range(1,10,1):
num = str(i)
value = int(num)
# Check if the current number
# is within range
if (value >= start and value <= end):
ans.append(value)
# Iterate on the digits starting
# from i
for j in range(i + 1,10,1):
num += str(j)
value = int(num)
# Checking the consecutive
# digits numbers starting
# from i and ending at j
# is within range or not
if (value >= start and value <= end):
ans.append(value)
# Sort the numbers in the
# increasing order
ans.sort()
return ans
# Driver Code
if __name__ == '__main__':
L = 12
R = 87
ans = solve(12, 87)
# Print the required numbers
for it in ans:
print(it,end = " ")
# This code is contributed by SURENDRA_GANGWAR.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class Program
{
// Function to find the consecutive
// digit numbers in the given range
static void solve(int start, int end)
{
// Initialize num as empty String
string num = "";
// Stores the resultant number
List ans = new List();
// Iterate over the range [1, 9]
for(int i = 1; i <= 9; i++)
{
num = i.ToString();
int value = i;
// Check if the current number
// is within range
if (value >= start && value <= end)
{
ans.Add(value);
}
// Iterate on the digits starting
// from i
for(int j = i + 1; j <= 9; j++)
{
num += j.ToString();
value = Int32.Parse(num);
// Checking the consecutive
// digits numbers starting
// from i and ending at j
// is within range or not
if (value >= start &&
value <= end)
{
ans.Add(value);
}
}
}
// Sort the numbers in the
// increasing order
ans.Sort();
// Print the required numbers
foreach(int it in ans)
Console.Write(it + " ");
}
// Driver code
static void Main() {
int L = 12, R = 87;
solve(L,R);
}
}
// This code is contributed by SoumikMondal
Javascript
输出:
12 23 34 45 56 67 78
时间复杂度: O(1)
空间复杂度: O(1)