📌  相关文章
📜  按给定范围打印所有数字,数字严格按升序排列

📅  最后修改于: 2021-04-27 23:34:25             🧑  作者: 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


输出:
12 13 14 15

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