给定两个正整数L和R ,任务是打印范围在[L,R]中的数字,这些数字的数字严格按升序排列。
例子:
Input: L = 10, R = 15
Output: 12 13 14 15
Explanation:
In the range [10, 15], only the numbers {12, 13, 14, 15} have their digits in strictly increasing order.
Input: L = 60, R = 70
Output: 67 68 69
Explanation:
In the range [60, 70], only the numbers {67, 68, 69} have their digits in strictly increasing order.
方法:想法是在[L,R]范围内进行迭代,并对该范围内的每个数字检查此数字的位数是否严格按照升序排列。如果是,则打印该号码,否则检查下一个号码。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to print all numbers
// in the range [L, R] having digits
// in strictly increasing order
void printNum(int L, int R)
{
// Iterate over the range
for (int i = L; i <= R; i++) {
int temp = i;
int c = 10;
int flag = 0;
// Iterate over the digits
while (temp > 0) {
// Check if the current digit
// is >= the previous digit
if (temp % 10 >= c) {
flag = 1;
break;
}
c = temp % 10;
temp /= 10;
}
// If the digits are in
// ascending order
if (flag == 0)
cout << i << " ";
}
}
// Driver Code
int main()
{
// Given range L and R
int L = 10, R = 15;
// Function Call
printNum(L, R);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to print all numbers
// in the range [L, R] having digits
// in strictly increasing order
static void printNum(int L, int R)
{
// Iterate over the range
for(int i = L; i <= R; i++)
{
int temp = i;
int c = 10;
int flag = 0;
// Iterate over the digits
while (temp > 0)
{
// Check if the current digit
// is >= the previous digit
if (temp % 10 >= c)
{
flag = 1;
break;
}
c = temp % 10;
temp /= 10;
}
// If the digits are in
// ascending order
if (flag == 0)
System.out.print(i + " ");
}
}
// Driver code
public static void main(String[] args)
{
// Given range L and R
int L = 10, R = 15;
// Function call
printNum(L, R);
}
}
// This code is contributed by offbeat
Python3
# Python3 program for the above approach
# Function to print all numbers in
# the range [L, R] having digits
# in strictly increasing order
def printNum(L, R):
# Iterate over the range
for i in range(L, R + 1):
temp = i
c = 10
flag = 0
# Iterate over the digits
while (temp > 0):
# Check if the current digit
# is >= the previous digit
if (temp % 10 >= c):
flag = 1
break
c = temp % 10
temp //= 10
# If the digits are in
# ascending order
if (flag == 0):
print(i, end = " ")
# Driver Code
# Given range L and R
L = 10
R = 15
# Function call
printNum(L, R)
# This code is contributed by code_hunt
C#
// C# program for the above approach
using System;
class GFG{
// Function to print all numbers
// in the range [L, R] having digits
// in strictly increasing order
static void printNum(int L, int R)
{
// Iterate over the range
for(int i = L; i <= R; i++)
{
int temp = i;
int c = 10;
int flag = 0;
// Iterate over the digits
while (temp > 0)
{
// Check if the current digit
// is >= the previous digit
if (temp % 10 >= c)
{
flag = 1;
break;
}
c = temp % 10;
temp /= 10;
}
// If the digits are in
// ascending order
if (flag == 0)
Console.Write(i + " ");
}
}
// Driver Code
public static void Main()
{
// Given range L and R
int L = 10, R = 15;
// Function call
printNum(L, R);
}
}
// This code is contributed by jrishabh99
输出:
12 13 14 15
时间复杂度O(N),N是L和R之间的绝对差。
辅助空间: O(1)