📜  打印给定长度的所有序列

📅  最后修改于: 2021-05-04 11:51:54             🧑  作者: Mango

给定两个整数k和n,编写一个函数,该函数打印所有由数字1,2..n组成的长度为k的序列。您需要按排序顺序打印这些序列。
例子:

Input: k = 2, n = 3

Output: 
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
Input:  k = 3, n = 4

Output: 
1 1 1
1 1 2
1 1 3
1 1 4
1 2 1
.....
.....
4 3 4
4 4 1
4 4 2
4 4 3
4 4 4

方法1(简单且迭代):
按排序顺序打印所有序列的简单想法是从{1 1…1}开始,并在序列不变为{nn…n}的情况下不断增加序列。以下是详细的过程。
1)创建一个大小为k的输出数组arr []。将数组初始化为{1,1…1}。
2)打印数组arr []。
3)更新数组arr [],使其成为其自身的直接后继(将被打印)。例如,{1,1,1}的直接后继是{1,1,2},{1,4,4}的直接后继是{2,1,1}和{4,4,3 }为{4 4 4}。
4)有后继阵列时,请重复步骤2和3。换句话说,虽然输出数组arr []不会变成{n,n .. n}
现在让我们讨论如何修改数组,使其包含直接后继数组。根据定义,直接后继项应具有相同的前p个项和较大的(p + 1)个项。在原始数组中,第(p + 1)个项(或arr [p])小于n,且所有在arr [p]之后的项,即arr [p + 1],arr [p + 2],…arr [ k-1]为n。
为了找到直接后继,我们在先前打印的数组中找到点p。为了找到点p,我们从最右边开始,一直移动直到找到一个数字arr [p],使得arr [p]小于n(或不小于n)。找到这样的点后,将arr [p]递增1,并将arr [p]之后的所有元素都设为1,即arr [p + 1] = 1,arr [p + 2] = 1。 。arr [k-1] = 1.以下是获取arr []的直接后继者的详细步骤。
1)从最右边的项arr [k-1]开始,然后向左移动。查找与n不相同的第一个元素arr [p]。
2)将arr [p]递增1
3)从arr [p + 1]到arr [k-1],将所有项的值设置为1。

C++
// CPP program of above approach
#include
 
/* A utility function that prints a given arr[] of length size*/
void printArray(int arr[], int size)
{
    for(int i = 0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
    return;
}
 
/* This function returns 0 if there are no more sequences to be printed, otherwise
   modifies arr[] so that arr[] contains next sequence to be printed */
int getSuccessor(int arr[], int k, int n)
{
    /* start from the rightmost side and find the first number less than n */
    int p = k - 1;
    while (arr[p] == n)
        p--;
 
    /* If all numbers are n in the array then there is no successor, return 0 */
    if (p < 0)
        return 0;
 
    /* Update arr[] so that it contains successor */
    arr[p] = arr[p] + 1;
    for(int i = p + 1; i < k; i++)
        arr[i] = 1;
 
    return 1;
}
 
/* The main function that prints all sequences from 1, 1, ..1 to n, n, ..n */
void printSequences(int n, int k)
{
    int *arr = new int[k];
 
    /* Initialize the current sequence as the first sequence to be printed */
    for(int i = 0; i < k; i++)
        arr[i] = 1;
 
    /* The loop breaks when there are no more successors to be printed */
    while(1)
    {
        /* Print the current sequence */
        printArray(arr, k);
 
        /* Update arr[] so that it contains next sequence to be printed. And if
           there are no more sequences then break the loop */
        if(getSuccessor(arr, k, n) == 0)
          break;
    }
 
    delete(arr); // free dynamically allocated array
    return;
}
 
/* Driver Program to test above functions */
int main()
{
    int n = 3;
    int k = 2;
    printSequences(n, k);
    return 0;
}


Java
// JAVA program of above approach
 
class GFG
{
 
    /* A utility function that prints a given arr[] of length size*/
    static void printArray(int arr[], int size)
    {
        for (int i = 0; i < size; i++)
        {
            System.out.printf("%d ", arr[i]);
        }
        System.out.printf("\n");
        return;
    }
 
    /* This function returns 0 if there are
    no more sequences to be printed, otherwise
    modifies arr[] so that arr[] contains
    next sequence to be printed */
    static int getSuccessor(int arr[], int k, int n)
    {
        /* start from the rightmost side and
        find the first number less than n */
        int p = k - 1;
        while (arr[p] == n)
        {
            p--;
            if (p < 0)
            {
                break;
            }
        }
 
        /* If all numbers are n in the array
        then there is no successor, return 0 */
        if (p < 0)
        {
            return 0;
        }
 
        /* Update arr[] so that it contains successor */
        arr[p] = arr[p] + 1;
        for (int i = p + 1; i < k; i++)
        {
            arr[i] = 1;
        }
        return 1;
    }
 
    /* The main function that prints all
    sequences from 1, 1, ..1 to n, n, ..n */
    static void printSequences(int n, int k)
    {
        int[] arr = new int[k];
 
        /* Initialize the current sequence as
        the first sequence to be printed */
        for (int i = 0; i < k; i++)
        {
            arr[i] = 1;
        }
 
        /* The loop breaks when there are
        no more successors to be printed */
        while (true)
        {
            /* Print the current sequence */
            printArray(arr, k);
 
            /* Update arr[] so that it contains
            next sequence to be printed. And if
            there are no more sequences then
            break the loop */
            if (getSuccessor(arr, k, n) == 0)
            {
                break;
            }
        }
    }
 
    /* Driver code */
    public static void main(String[] args)
    {
        int n = 3;
        int k = 2;
        printSequences(n, k);
    }
}
 
// This code is contributed by PrinciRaj1992


Python3
# Python3 program of above approach
 
# A utility function that prints
# a given arr[] of length size#
def printArray(arr, size):
    for i in range(size):
        print(arr[i], end = " ")
    print()
    return
 
# This function returns 0 if there are
# no more sequences to be printed, otherwise
# modifies arr[] so that arr[] contains
# next sequence to be printed #
def getSuccessor(arr, k, n):
     
    # start from the rightmost side and
    # find the first number less than n
    p = k - 1
    while (arr[p] == n and 0 <= p < k):
        p -= 1
         
    # If all numbers are n in the array
    # then there is no successor, return 0
    if (p < 0):
        return 0
         
    # Update arr[] so that it contains successor
    arr[p] = arr[p] + 1
    i = p + 1
    while(i < k):
        arr[i] = 1
        i += 1
    return 1
     
# The main function that prints all sequences
# from 1, 1, ..1 to n, n, ..n
def printSequences(n, k):
    arr = [0] * k
     
    # Initialize the current sequence as
    # the first sequence to be printed #
    for i in range(k):
        arr[i] = 1
         
    # The loop breaks when there are
    # no more successors to be printed
    while(1):
         
        # Print the current sequence
        printArray(arr, k)
         
        # Update arr[] so that it contains
        # next sequence to be printed. And if
        # there are no more sequences then
        # break the loop
        if(getSuccessor(arr, k, n) == 0):
            break
    return
 
# Driver code
n = 3
k = 2
printSequences(n, k)
 
# This code is contributed by shubhamsingh10


C#
// C# program of above approach
using System;
 
class GFG
{
 
    /* A utility function that prints
    a given []arr of length size*/
    static void printArray(int []arr, int size)
    {
        for (int i = 0; i < size; i++)
        {
            Console.Write("{0} ", arr[i]);
        }
        Console.Write("\n");
        return;
    }
 
    /* This function returns 0 if there are
    no more sequences to be printed, otherwise
    modifies []arr so that []arr contains
    next sequence to be printed */
    static int getSuccessor(int []arr, int k, int n)
    {
        /* start from the rightmost side and
        find the first number less than n */
        int p = k - 1;
        while (arr[p] == n)
        {
            p--;
            if (p < 0)
            {
                break;
            }
        }
 
        /* If all numbers are n in the array
        then there is no successor, return 0 */
        if (p < 0)
        {
            return 0;
        }
 
        /* Update []arr so that it contains successor */
        arr[p] = arr[p] + 1;
        for (int i = p + 1; i < k; i++)
        {
            arr[i] = 1;
        }
        return 1;
    }
 
    /* The main function that prints all
    sequences from 1, 1, ..1 to n, n, ..n */
    static void printSequences(int n, int k)
    {
        int[] arr = new int[k];
 
        /* Initialize the current sequence as
        the first sequence to be printed */
        for (int i = 0; i < k; i++)
        {
            arr[i] = 1;
        }
 
        /* The loop breaks when there are
        no more successors to be printed */
        while (true)
        {
            /* Print the current sequence */
            printArray(arr, k);
 
            /* Update []arr so that it contains
            next sequence to be printed. And if
            there are no more sequences then
            break the loop */
            if (getSuccessor(arr, k, n) == 0)
            {
                break;
            }
        }
    }
 
    /* Driver code */
    public static void Main(String[] args)
    {
        int n = 3;
        int k = 2;
        printSequences(n, k);
    }
}
 
// This code is contributed by Rajput-Ji


Javascript


C++
// C++ program of above approach
#include
 
/* A utility function that prints a given arr[] of length size*/
void printArray(int arr[], int size)
{
    for(int i = 0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
    return;
}
 
/* The core function that recursively generates and prints all sequences of
  length k */
void printSequencesRecur(int arr[], int n, int k, int index)
{
   int i;
   if (k == 0)
   {
     printArray(arr, index);
   }
   if (k > 0)
   {
      for(i = 1; i<=n; ++i)
      {
        arr[index] = i;
        printSequencesRecur(arr, n, k-1, index+1);
      }
   }
}
 
 
/* A function that uses printSequencesRecur() to prints all sequences
   from 1, 1, ..1 to n, n, ..n */
void printSequences(int n, int k)
{
    int *arr = new int[k];
    printSequencesRecur(arr, n, k, 0);
 
    delete(arr); // free dynamically allocated array
    return;
}
 
/* Driver Program to test above functions */
int main()
{
    int n = 3;
    int k = 2;
    printSequences(n, k);
    return 0;
}


Java
// Java program of above approach
 
class GfG {
 
/* A utility function that prints a given arr[] of length size*/
static void printArray(int arr[], int size)
{
    for(int i = 0; i < size; i++)
        System.out.print(arr[i] + " ");
System.out.println();
    return;
}
 
/* The core function that recursively generates and prints all sequences of
length k */
static void printSequencesRecur(int arr[], int n, int k, int index)
{
int i;
if (k == 0)
{
    printArray(arr, index);
}
if (k > 0)
{
    for(i = 1; i<=n; ++i)
    {
        arr[index] = i;
        printSequencesRecur(arr, n, k-1, index+1);
    }
}
}
 
 
/* A function that uses printSequencesRecur() to prints all sequences
from 1, 1, ..1 to n, n, ..n */
static void printSequences(int n, int k)
{
    int arr[] = new int[k];
    printSequencesRecur(arr, n, k, 0);
 
return ;
}
 
/* Driver Program to test above functions */
public static void main(String[] args)
{
    int n = 3;
    int k = 2;
    printSequences(n, k);
}
}


Python3
# Python3 program of above approach
 
# A utility function that prints a
# given arr[] of length size
def printArray(arr, size):
 
    for i in range(size):
        print(arr[i], end = " ");
    print("");
    return;
 
# The core function that recursively
# generates and prints all sequences
# of length k
def printSequencesRecur(arr, n, k, index):
    if (k == 0):
        printArray(arr, index);
 
    if (k > 0):
        for i in range(1, n + 1):
            arr[index] = i;
            printSequencesRecur(arr, n, k - 1,
                                    index + 1);
 
# A function that uses printSequencesRecur() to
# prints all sequences from 1, 1, ..1 to n, n, ..n
def printSequences(n, k):
    arr = [0] * n;
    printSequencesRecur(arr, n, k, 0);
 
    return;
 
# Driver Code
n = 3;
k = 2;
printSequences(n, k);
 
# This code is contributed mits


C#
// C# program of above approach
using System;
 
class GFG
{
 
/* A utility function that prints a given
arr[] of length size*/
static void printArray(int []arr, int size)
{
    for(int i = 0; i < size; i++)
        Console.Write(arr[i] + " ");
    Console.WriteLine();
    return;
}
 
/* The core function that recursively generates
and prints all sequences of length k */
static void printSequencesRecur(int []arr, int n,
                                int k, int index)
{
    int i;
    if (k == 0)
    {
        printArray(arr, index);
    }
    if (k > 0)
    {
        for(i = 1; i<=n; ++i)
        {
            arr[index] = i;
            printSequencesRecur(arr, n, k - 1, index + 1);
        }
    }
}
 
 
/* A function that uses printSequencesRecur() to
prints all sequences from 1, 1, ..1 to n, n, ..n */
static void printSequences(int n, int k)
{
    int[] arr = new int[k];
    printSequencesRecur(arr, n, k, 0);
 
    return;
}
 
// Driver Code
public static void Main()
{
    int n = 3;
    int k = 2;
    printSequences(n, k);
}
}
 
// This code is contributed
// by Akanksha Rai


PHP
 0)
    {
        for($i = 1; $i <= $n; ++$i)
        {
            $arr[$index] = $i;
            printSequencesRecur($arr, $n,
                                $k - 1, $index + 1);
        }
    }
}
 
 
/* A function that uses printSequencesRecur() to
prints all sequences from 1, 1, ..1 to n, n, ..n */
function printSequences($n, $k)
{
    $arr = array();
    printSequencesRecur($arr, $n, $k, 0);
 
    return;
}
 
// Driver Code
$n = 3;
$k = 2;
printSequences($n, $k);
 
// This code is contributed
// by Akanksha Rai
?>


Javascript


C++
// C++ program of above approach
 
/* The core function that generates and
prints all sequences of length k */
void printSeqRecur(int num, int pos, int k, int n)
{
    if (pos == k)
    {
        cout << num << endl;
        return;
    }
    for (int i = 1; i <= n; i++)
    {
        printSeqRecur(num * 10 + i, pos + 1, k, n);
    }
}
 
/* A function that uses printSequencesRecur()
to prints all sequences
from 1, 1, ..1 to n, n, ..n */
void printSequences(int k, int n)
{
    printSeqRecur(0, 0, k, n);
}
 
// This code is contributed by SHUBHAMSINGH10


C
// C program of above approach
/* The core function that generates and prints all sequences of length k */
void printSeqRecur(int num, int pos, int k, int n)
{
    if (pos == k) {
        printf("%d \n", num);
        return;
    }
    for (int i = 1; i <= n; i++) {
        printSeqRecur(num * 10 + i, pos + 1, k, n);
    }
}
 
/* A function that uses printSequencesRecur() to prints all sequences
   from 1, 1, ..1 to n, n, ..n */
void printSequences(int k, int n)
{
    printSeqRecur(0, 0, k, n);
}


Java
// Java program of above approach
 
/* The core function that generates and prints all sequences of length k */
static void printSeqRecur(int num, int pos, int k, int n)
{
    if (pos == k) {
        System.out.print(num + " ");
        return;
    }
    for (int i = 1; i <= n; i++) {
        printSeqRecur(num * 10 + i, pos + 1, k, n);
    }
}
 
/* A function that uses printSequencesRecur() to prints all sequences
from 1, 1, ..1 to n, n, ..n */
static void printSequences(int k, int n)
{
    printSeqRecur(0, 0, k, n);
}


Python3
# Python program of above approach
# We have used number instead of
# arrays to prevent linear time
# required to print each string
def printSeqRecur ( num , n, k ):
    if n == 0: # if total digits become equal
                # to n, print the number and return
        print(num )
        return
         
    for _ in range(1, k + 1):
        printSeqRecur (num * 10 + _, n - 1, k)
 
# Driver Code
if __name__ == "__main__":
    k = 3 # length of k-ary string
    n = 2 # string can take values
          # from 1,2,3...n
    printSeqRecur(0, n, k)
 
# This code is contributed
# by shivam purohit


C#
// C# program of above approach
 
/* The core function that generates
and prints all sequences of length k */
static void printSeqRecur(int num, int pos,
                            int k, int n)
{
    if (pos == k)
    {
        Console.Write(num + " ");
        return;
    }
    for (int i = 1; i <= n; i++)
    {
        printSeqRecur(num * 10 + i, pos + 1, k, n);
    }
}
 
/* A function that uses printSequencesRecur()
to prints all sequences from 1, 1, ..1 to n, n, ..n */
static void printSequences(int k, int n)
{
    printSeqRecur(0, 0, k, n);
}
 
// This code is contributed by Code_Mech


PHP


输出:

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

时间复杂度:总共有n ^ k个序列。打印序列并找到其后继者需要O(k)时间。因此上述实现的时间复杂度为O(k * n ^ k)。
方法2(棘手和递归)
递归函数printSequencesRecur生成并打印所有长度为k的序列。想法是再使用一个参数索引。函数printSequencesRecur将所有术语保持在arr []内直到索引有效,更新索引上的值,并在索引之后递归调用自身以获取更多术语。

C++

// C++ program of above approach
#include
 
/* A utility function that prints a given arr[] of length size*/
void printArray(int arr[], int size)
{
    for(int i = 0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
    return;
}
 
/* The core function that recursively generates and prints all sequences of
  length k */
void printSequencesRecur(int arr[], int n, int k, int index)
{
   int i;
   if (k == 0)
   {
     printArray(arr, index);
   }
   if (k > 0)
   {
      for(i = 1; i<=n; ++i)
      {
        arr[index] = i;
        printSequencesRecur(arr, n, k-1, index+1);
      }
   }
}
 
 
/* A function that uses printSequencesRecur() to prints all sequences
   from 1, 1, ..1 to n, n, ..n */
void printSequences(int n, int k)
{
    int *arr = new int[k];
    printSequencesRecur(arr, n, k, 0);
 
    delete(arr); // free dynamically allocated array
    return;
}
 
/* Driver Program to test above functions */
int main()
{
    int n = 3;
    int k = 2;
    printSequences(n, k);
    return 0;
}

Java

// Java program of above approach
 
class GfG {
 
/* A utility function that prints a given arr[] of length size*/
static void printArray(int arr[], int size)
{
    for(int i = 0; i < size; i++)
        System.out.print(arr[i] + " ");
System.out.println();
    return;
}
 
/* The core function that recursively generates and prints all sequences of
length k */
static void printSequencesRecur(int arr[], int n, int k, int index)
{
int i;
if (k == 0)
{
    printArray(arr, index);
}
if (k > 0)
{
    for(i = 1; i<=n; ++i)
    {
        arr[index] = i;
        printSequencesRecur(arr, n, k-1, index+1);
    }
}
}
 
 
/* A function that uses printSequencesRecur() to prints all sequences
from 1, 1, ..1 to n, n, ..n */
static void printSequences(int n, int k)
{
    int arr[] = new int[k];
    printSequencesRecur(arr, n, k, 0);
 
return ;
}
 
/* Driver Program to test above functions */
public static void main(String[] args)
{
    int n = 3;
    int k = 2;
    printSequences(n, k);
}
}

Python3

# Python3 program of above approach
 
# A utility function that prints a
# given arr[] of length size
def printArray(arr, size):
 
    for i in range(size):
        print(arr[i], end = " ");
    print("");
    return;
 
# The core function that recursively
# generates and prints all sequences
# of length k
def printSequencesRecur(arr, n, k, index):
    if (k == 0):
        printArray(arr, index);
 
    if (k > 0):
        for i in range(1, n + 1):
            arr[index] = i;
            printSequencesRecur(arr, n, k - 1,
                                    index + 1);
 
# A function that uses printSequencesRecur() to
# prints all sequences from 1, 1, ..1 to n, n, ..n
def printSequences(n, k):
    arr = [0] * n;
    printSequencesRecur(arr, n, k, 0);
 
    return;
 
# Driver Code
n = 3;
k = 2;
printSequences(n, k);
 
# This code is contributed mits

C#

// C# program of above approach
using System;
 
class GFG
{
 
/* A utility function that prints a given
arr[] of length size*/
static void printArray(int []arr, int size)
{
    for(int i = 0; i < size; i++)
        Console.Write(arr[i] + " ");
    Console.WriteLine();
    return;
}
 
/* The core function that recursively generates
and prints all sequences of length k */
static void printSequencesRecur(int []arr, int n,
                                int k, int index)
{
    int i;
    if (k == 0)
    {
        printArray(arr, index);
    }
    if (k > 0)
    {
        for(i = 1; i<=n; ++i)
        {
            arr[index] = i;
            printSequencesRecur(arr, n, k - 1, index + 1);
        }
    }
}
 
 
/* A function that uses printSequencesRecur() to
prints all sequences from 1, 1, ..1 to n, n, ..n */
static void printSequences(int n, int k)
{
    int[] arr = new int[k];
    printSequencesRecur(arr, n, k, 0);
 
    return;
}
 
// Driver Code
public static void Main()
{
    int n = 3;
    int k = 2;
    printSequences(n, k);
}
}
 
// This code is contributed
// by Akanksha Rai

的PHP

 0)
    {
        for($i = 1; $i <= $n; ++$i)
        {
            $arr[$index] = $i;
            printSequencesRecur($arr, $n,
                                $k - 1, $index + 1);
        }
    }
}
 
 
/* A function that uses printSequencesRecur() to
prints all sequences from 1, 1, ..1 to n, n, ..n */
function printSequences($n, $k)
{
    $arr = array();
    printSequencesRecur($arr, $n, $k, 0);
 
    return;
}
 
// Driver Code
$n = 3;
$k = 2;
printSequences($n, $k);
 
// This code is contributed
// by Akanksha Rai
?>

Java脚本


输出:

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3

时间复杂度:有n ^ k个序列,打印一个序列需要O(k)时间。因此,时间复杂度为O(k * n ^ k)。
感谢mopurizwarriors建议上述方法。正如alphayoung所建议的那样,我们可以避免将数组用于适合整数的小序列。以下是相同的实现。

C++

// C++ program of above approach
 
/* The core function that generates and
prints all sequences of length k */
void printSeqRecur(int num, int pos, int k, int n)
{
    if (pos == k)
    {
        cout << num << endl;
        return;
    }
    for (int i = 1; i <= n; i++)
    {
        printSeqRecur(num * 10 + i, pos + 1, k, n);
    }
}
 
/* A function that uses printSequencesRecur()
to prints all sequences
from 1, 1, ..1 to n, n, ..n */
void printSequences(int k, int n)
{
    printSeqRecur(0, 0, k, n);
}
 
// This code is contributed by SHUBHAMSINGH10

C

// C program of above approach
/* The core function that generates and prints all sequences of length k */
void printSeqRecur(int num, int pos, int k, int n)
{
    if (pos == k) {
        printf("%d \n", num);
        return;
    }
    for (int i = 1; i <= n; i++) {
        printSeqRecur(num * 10 + i, pos + 1, k, n);
    }
}
 
/* A function that uses printSequencesRecur() to prints all sequences
   from 1, 1, ..1 to n, n, ..n */
void printSequences(int k, int n)
{
    printSeqRecur(0, 0, k, n);
}

Java

// Java program of above approach
 
/* The core function that generates and prints all sequences of length k */
static void printSeqRecur(int num, int pos, int k, int n)
{
    if (pos == k) {
        System.out.print(num + " ");
        return;
    }
    for (int i = 1; i <= n; i++) {
        printSeqRecur(num * 10 + i, pos + 1, k, n);
    }
}
 
/* A function that uses printSequencesRecur() to prints all sequences
from 1, 1, ..1 to n, n, ..n */
static void printSequences(int k, int n)
{
    printSeqRecur(0, 0, k, n);
}

Python3

# Python program of above approach
# We have used number instead of
# arrays to prevent linear time
# required to print each string
def printSeqRecur ( num , n, k ):
    if n == 0: # if total digits become equal
                # to n, print the number and return
        print(num )
        return
         
    for _ in range(1, k + 1):
        printSeqRecur (num * 10 + _, n - 1, k)
 
# Driver Code
if __name__ == "__main__":
    k = 3 # length of k-ary string
    n = 2 # string can take values
          # from 1,2,3...n
    printSeqRecur(0, n, k)
 
# This code is contributed
# by shivam purohit

C#

// C# program of above approach
 
/* The core function that generates
and prints all sequences of length k */
static void printSeqRecur(int num, int pos,
                            int k, int n)
{
    if (pos == k)
    {
        Console.Write(num + " ");
        return;
    }
    for (int i = 1; i <= n; i++)
    {
        printSeqRecur(num * 10 + i, pos + 1, k, n);
    }
}
 
/* A function that uses printSequencesRecur()
to prints all sequences from 1, 1, ..1 to n, n, ..n */
static void printSequences(int k, int n)
{
    printSeqRecur(0, 0, k, n);
}
 
// This code is contributed by Code_Mech

的PHP