📜  生成数字严格按升序排列的k位数字

📅  最后修改于: 2021-04-29 11:07:39             🧑  作者: Mango

给定值k,则生成长度为k的所有有序数。井井有条意味着数字应该不断增加。

例子 :

Input : K = 7
Output :
1234567 1234568 1234569 1234578 1234579
1234589 1234678 1234679 1234689 1234789 
1235678 1235679 1235689 1235789 1236789 
1245678 1245679 1245689 1245789 1246789 
1256789 1345678 1345679 1345689 1345789 
1346789 1356789 1456789 2345678 2345679 
2345689 2345789 2346789 2356789 2456789 
3456789

Input  : K = 3
Output :
123 124 125 126 127 128 129 134 135 136 
137 138 139 145 146 147 148 149 156 157 
158 159 167 168 169 178 179 189 234 235 
236 237 238 239 245 246 247 248 249 256 
257 258 259 267 268 269 278 279 289 345 
346 347 348 349 356 357 358 359 367 368 
369 378 379 389 456 457 458 459 467 468 
469 478 479 489 567 568 569 578 579 589 
678 679 689 789 

此问题与打印所有可能由一组n个字符组成的长度为k的字符串非常相似

1.循环遍历i = x到9。(所有数字从1到9)。
2. x将从0开始,并在每次递归调用时递增,以确保数字格式正确。
3.对于每个递归调用,将结果(将从0开始)乘以10,然后加上i,使k = k-1。

下面是上述想法的实现:

C++
// C++ program to generate well
// ordered numbers with k digits.
#include
using namespace std;
  
// number --> Current value of number.
// x --> Current digit to be considered
// k --> Remaining number of digits
void printWellOrdered(int number, 
                      int x, int k)
{
    if (k == 0)
    {
        cout << number << " ";
        return;
    }
  
    // Try all possible greater digits
    for (int i = (x + 1); i < 10; i++)
        printWellOrdered(number * 10 + 
                         i, i, k - 1);
}
  
// Generates all well ordered 
// numbers of length k.
void generateWellOrdered(int k)
{
    printWellOrdered(0, 0, k);
}
  
// Driver code
int main()
{
    int k = 3;
    generateWellOrdered(k);
    return 0;
}


Java
// Java program to generate well 
// ordered numbers with k digits.
  
class Generate
{
    // number --> Current value of number.
    // x --> Current digit to be considered
    // k --> Remaining number of digits
    static void printWellOrdered(int number, 
                                 int x, int k)
    {
        if (k == 0)
        {
            System.out.print(number+" ");
            return;
        }
      
        // Try all possible greater digits
        for (int i = (x + 1); i < 10; i++)
            printWellOrdered(number * 10 + 
                             i, i, k - 1);
    }
      
    // Generates all well ordered 
    // numbers of length k.
    static void generateWellOrdered(int k)
    {
        printWellOrdered(0, 0, k);
    }
      
    // Driver Code 
    public static void main (String[] args) 
    {
        int k = 3;
        generateWellOrdered(k);
    }
}


Python 3
# Python 3 program to generate well
# ordered numbers with k digits.
  
# number --> Current value of number.
# x --> Current digit to be considered
# k --> Remaining number of digits
def printWellOrdered(number, x, k):
  
    if (k == 0):
        print(number, end = " ")
        return
  
    # Try all possible greater digits
    for i in range( (x + 1), 10):
        printWellOrdered(number * 10 + i,
                                i, k - 1)
  
# Generates all well ordered 
# numbers of length k.
def generateWellOrdered(k):
    printWellOrdered(0, 0, k)
  
# Driver code
if __name__ == "__main__":
      
    k = 3
    generateWellOrdered(k)
  
# This code is contributed by Ita_c


C#
// C# program to generate well
// ordered numbers with k digits.
using System;
  
class GFG 
{
      
    // number --> Current value of number.
    // x --> Current digit to be considered
    // k --> Remaining number of digits
    static void printWellOrdered(int number, 
                                 int x, int k)
    {
          
        if (k == 0)
        {
            Console.Write(number + " ");
            return;
        }
      
        // Try all possible greater digits
        for (int i = (x + 1); i < 10; i++)
            printWellOrdered(number * 10 + 
                             i, i, k - 1);
    }
      
    // Generates all well ordered 
    // numbers of length k.
    static void generateWellOrdered(int k)
    {
        printWellOrdered(0, 0, k);
    }
      
    // Driver Code
    public static void Main () 
    {
        int k = 3;
        generateWellOrdered(k);
    }
}
  
// This code is contributed by nitin mittal


PHP
 Current value of number.
// x --> Current digit to be considered
// k --> Remaining number of digits
function printWellOrdered($number, $x, $k)
{
    if ($k == 0)
    {
        echo $number, " ";
        return;
    }
  
    // Try all possible greater digits
    for ($i = ($x + 1); $i < 10; $i++)
        printWellOrdered($number * 10 + 
                         $i, $i, $k - 1);
}
  
// Generates all well ordered 
// numbers of length k
function generateWellOrdered($k)
{
    printWellOrdered(0, 0, $k);
}
  
// Driver code
$k = 3;
generateWellOrdered($k);
  
// This code is contributed by ajit.
?>


输出 :

123 124 125 126 127 128 129 134 135 136 137 
138 139 145 146 147 148 149 156 157 158 159 
167 168 169 178 179 189 234 235 236 237 238 
239 245 246 247 248 249 256 257 258 259 267 
268 269 278 279 289 345 346 347 348 349 356 
357 358 359 367 368 369 378 379 389 456 457 
458 459 467 468 469 478 479 489 567 568 569 
578 579 589 678 679 689 789