📌  相关文章
📜  以严格递增的顺序打印给定范围内的所有数字

📅  最后修改于: 2021-10-27 06:24:29             🧑  作者: Mango

给定两个正整数LR ,任务是打印范围[L, R]中的数字,这些数字的数字严格按递增顺序排列。

例子:

方法:这个想法是迭代范围[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


Javascript


输出:
12 13 14 15

时间复杂度O(N),N 是 L 和 R 之间的绝对差。
辅助空间: O(1)