原文:

给定两个正整数 n 和 k,打印所有长度为 k 的递增序列,使得每个序列中的元素都来自前 n 个自然数。

示例:

input: k = 2, n = 3
output: 1 2
        1 3
        2 3 
input: k = 5, n = 5
output: 1 2 3 4 5
input: k = 3, n = 5
output: 1 2 3
        1 2 4
        1 2 5
        1 3 4
        1 3 5
        1 4 5
        2 3 4
        2 3 5
        2 4 5
        3 4 5

强烈建议尽量减少浏览器,先自己试试这个。 这是一个很好的递归问题。其思想是创建一个长度为 k 的数组。该数组存储当前序列。对于数组中的每个位置,我们检查前一个元素,并一个接一个地放置大于前一个元素的所有元素。如果没有前一个元素(第一个位置),我们把所有的数字从 1 到 n。

以下是上述想法的实现:

c

// c   program to  print all increasing sequences of
// length 'k' such that the elements in every sequence
// are from first 'n' natural numbers.
#include
using namespace std;
// a utility function to print contents of arr[0..k-1]
void printarr(int arr[], int k)
{
    for (int i=0; i

java 语言(一种计算机语言,尤用于创建网站)

// java program to print all
// increasing sequences of
// length 'k' such that the
// elements in every sequence
// are from first 'n'
// natural numbers.
class gfg {
    // a utility function to print
    // contents of arr[0..k-1]
    static void printarr(int[] arr, int k)
    {
        for (int i = 0; i < k; i  )
            system.out.print(arr[i]   " ");
        system.out.print("\n");
    }
    // a recursive function to print
    // all increasing sequences
    // of first n natural numbers.
    // every sequence should be
    // length k. the array arr[] is
    // used to store current sequence
    static void printsequtil(int n, int k,
                             int len, int[] arr)
    {
        // if length of current increasing
        // sequence becomes k, print it
        if (len == k)
        {
            printarr(arr, k);
            return;
        }
        // decide the starting number
        // to put at current position:
        // if length is 0, then there
        // are no previous elements
        // in arr[]. so start putting
        // new numbers with 1.
        // if length is not 0,
        // then start from value of
        // previous element plus 1.
        int i = (len == 0) ? 1 : arr[len - 1]   1;
        // increase length
        len  ;
        // put all numbers (which are
        // greater than the previous
        // element) at new position.
        while (i <= n)
        {
            arr[len - 1] = i;
            printsequtil(n, k, len, arr);
            i  ;
        }
        // this is important. the
        // variable 'len' is shared among
        // all function calls in recursion
        // tree. its value must be
        // brought back before next
        // iteration of while loop
        len--;
    }
    // this function prints all
    // increasing sequences of
    // first n natural numbers.
    // the length of every sequence
    // must be k. this function
    // mainly uses printsequtil()
    static void printseq(int n, int k)
    {
        // an array to store
        // individual sequences
        int[] arr = new int[k];
        // initial length of
        // current sequence
        int len = 0;
        printsequtil(n, k, len, arr);
    }
    // driver code
    static public void main (string[] args)
    {
        int k = 3, n = 7;
        printseq(n, k);
    }
}
// this code is contributed by smitha.

python 3

# python3 program to print all
# increasing sequences of length
# 'k' such that the elements in
# every sequence are from first
# 'n' natural numbers.
# a utility function to
# print contents of arr[0..k-1]
def printarr(arr, k):
    for i in range(k):
        print(arr[i], end = " ");
    print();
# a recursive function to print
# all increasing sequences of
# first n natural numbers. every
# sequence should be length k.
# the array arr[] is used to
# store current sequence.
def printsequtil(n, k,len1, arr):
    # if length of current
    # increasing sequence
    # becomes k, print it
    if (len1 == k):
        printarr(arr, k);
        return;
    # decide the starting number
    # to put at current position:
    # if length is 0, then there
    # are no previous elements
    # in arr[]. so start putting
    # new numbers with 1\. if length
    # is not 0, then start from value
    # of previous element plus 1.
    i = 1 if(len1 == 0) else (arr[len1 - 1]   1);
    # increase length
    len1  = 1;
    # put all numbers (which are greater
    # than the previous element) at
    # new position.
    while (i <= n):
        arr[len1 - 1] = i;
        printsequtil(n, k, len1, arr);
        i  = 1;
    # this is important. the variable
    # 'len' is shared among all function
    # calls in recursion tree. its value
    # must be brought back before next
    # iteration of while loop
    len1 -= 1;
# this function prints all increasing
# sequences of first n natural numbers.
# the length of every sequence must be
# k. this function mainly uses printsequtil()
def printseq(n, k):
        arr = [0] * k; # an array to store
                       # individual sequences
        len1 = 0; # initial length of
                  # current sequence
        printsequtil(n, k, len1, arr);
# driver code
k = 3;
n = 7;
printseq(n, k);
# this code is contributed by mits

c

// c# program to print all
// increasing sequences of
// length 'k' such that the
// elements in every sequence
// are from first 'n'
// natural numbers.
using system;
class gfg {
    // a utility function to print
    // contents of arr[0..k-1]
    static void printarr(int[] arr, int k)
    {
        for (int i = 0; i < k; i  )
            console.write(arr[i]   " ");
        console.writeline();
    }
    // a recursive function to print
    // all increasing sequences
    // of first n natural numbers.
    // every sequence should be
    // length k. the array arr[] is
    // used to store current sequence
    static void printsequtil(int n, int k,
                             int len, int[] arr)
    {
        // if length of current increasing
        // sequence becomes k, print it
        if (len == k)
        {
            printarr(arr, k);
            return;
        }
        // decide the starting number
        // to put at current position:
        // if length is 0, then there
        // are no previous elements
        // in arr[]. so start putting
        // new numbers with 1.
        // if length is not 0,
        // then start from value of
        // previous element plus 1.
        int i = (len == 0) ? 1 : arr[len - 1]   1;
        // increase length
        len  ;
        // put all numbers (which are
        // greater than the previous
        // element) at new position.
        while (i <= n)
        {
            arr[len - 1] = i;
            printsequtil(n, k, len, arr);
            i  ;
        }
        // this is important. the
        // variable 'len' is shared among
        // all function calls in recursion
        // tree. its value must be
        // brought back before next
        // iteration of while loop
        len--;
    }
    // this function prints all
    // increasing sequences of
    // first n natural numbers.
    // the length of every sequence
    // must be k. this function
    // mainly uses printsequtil()
    static void printseq(int n, int k)
    {
        // an array to store
        // individual sequences
        int[] arr = new int[k];
        // initial length of
        // current sequence
        int len = 0;
        printsequtil(n, k, len, arr);
    }
    // driver code
    static public void main ()
    {
        int k = 3, n = 7;
        printseq(n, k);
    }
}
// this code is contributed by ajit.

服务器端编程语言(professional hypertext preprocessor 的缩写)


java 描述语言


输出:

1 2 3
1 2 4
1 2 5
1 2 6
1 2 7
1 3 4
1 3 5
1 3 6
1 3 7
1 4 5
1 4 6
1 4 7
1 5 6
1 5 7
1 6 7
2 3 4
2 3 5
2 3 6
2 3 7
2 4 5
2 4 6
2 4 7
2 5 6
2 5 7
2 6 7
3 4 5
3 4 6
3 4 7
3 5 6
3 5 7
3 6 7
4 5 6
4 5 7
4 6 7
5 6 7

本文由阿琼供稿。如果你发现任何不正确的地方,请写评论,或者你想分享更多关于上面讨论的话题的信息