原文:

给定一个由个唯一元素组成的数组,我们必须使用数组的元素找到所有长度的排列 l 。允许重复元素。 例:

输入: arr = { 1,2 },l=3 输出: 111 211 121 221 112 212 122 222 输入: arr = { 1,2,3 },l=2 输出: 11

进场:

  • 为了用 n 个元素形成长度为 l 的序列,已知序列的第i个元素可以用 n 种方式填充。所以会有序列
  • 我们将运行一个从 0 到的循环,对于每个 i ,我们将把 i 从基地 10 转换为基地 n 。转换后的数字将代表数组的索引
  • 我们可以通过这种方式打印所有的序列。

以下是实施办法:

c

// c   implementation
#include 
using namespace std;
// convert the number to lth
// base and print the sequence
void convert_to_len_th_base(int n,
                            int arr[],
                            int len,
                            int l)
{
    // sequence is of length l
    for (int i = 0; i < l; i  ) {
        // print the ith element
        // of sequence
        cout << arr[n % len];
        n /= len;
    }
    cout << endl;
}
// print all the permuataions
void print(int arr[],
           int len,
           int l)
{
    // there can be (len)^l
    // permutations
    for (int i = 0; i < (int)pow(len, l); i  ) {
        // convert i to len th base
        convert_to_len_th_base(i, arr, len, l);
    }
}
// driver code
int main()
{
    int arr[] = { 1, 2, 3 };
    int len = sizeof(arr) / sizeof(arr[0]);
    int l = 2;
    // function call
    print(arr, len, l);
    return 0;
}

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

// java implementation for above approach
import java.io.*;
class gfg
{
// convert the number to lth
// base and print the sequence
static void convert_to_len_th_base(int n, int arr[],
                                   int len, int l)
{
    // sequence is of length l
    for (int i = 0; i < l; i  )
    {
        // print the ith element
        // of sequence
        system.out.print(arr[n % len]);
        n /= len;
    }
    system.out.println();
}
// print all the permuataions
static void print(int arr[], int len, int l)
{
    // there can be (len)^l
    // permutations
    for (int i = 0;
             i < (int)math.pow(len, l); i  )
    {
        // convert i to len th base
        convert_to_len_th_base(i, arr, len, l);
    }
}
// driver code
public static void main (string[] args)
{
    int arr[] = { 1, 2, 3 };
    int len = arr.length;
    int l = 2;
    // function call
    print(arr, len, l);
}
}
// this code is contributed by ajit.

python 3

# python3 implementation for the above approach
# convert the number to lth
# base and print the sequence
def convert_to_len_th_base(n, arr, len, l):
    # sequence is of length l
    for i in range(l):
        # print the ith element
        # of sequence
        print(arr[n % len], end = "")
        n //= len
    print()
# print all the permuataions
def printf(arr, len, l):
    # there can be (len)^l permutations
    for i in range(pow(len, l)):
        # convert i to len th base
        convert_to_len_th_base(i, arr, len, l)
# driver code
arr = [1, 2, 3]
len = len(arr)
l = 2
# function call
printf(arr, len, l)
# this code is contributed by mohit kumar

c

// c# implementation for above approach
using system;
class gfg
{
// convert the number to lth
// base and print the sequence
static void convert_to_len_th_base(int n, int []arr,
                                   int len, int l)
{
    // sequence is of length l
    for (int i = 0; i < l; i  )
    {
        // print the ith element
        // of sequence
        console.write(arr[n % len]);
        n /= len;
    }
    console.writeline();
}
// print all the permuataions
static void print(int []arr, int len, int l)
{
    // there can be (len)^l
    // permutations
    for (int i = 0;
            i < (int)math.pow(len, l); i  )
    {
        // convert i to len th base
        convert_to_len_th_base(i, arr, len, l);
    }
}
// driver code
public static void main (string[] args)
{
    int []arr = { 1, 2, 3 };
    int len = arr.length;
    int l = 2;
    // function call
    print(arr, len, l);
}
}
// this code is contributed by rajput-ji

java 描述语言


output: 

11
21
31
12
22
32
13
23
33