给定大小为n的数组,生成并打印数组中r个元素的所有可能组合。例如,如果输入数组为{1、2、3、4},且r为2,则输出应为{1、2},{1、3},{1、4},{2、3},{ 2,4}和{3,4}。
以下是执行此操作的两种方法。
方法1(修复元素并重复出现)
我们创建了一个临时数组’data []’,它逐一存储所有输出。这个想法是从data []中的第一个索引(index = 0)开始,在该索引处一个接一个的固定元素,然后对剩余的索引进行递归。令输入数组为{1、2、3、4、5},r为3。我们首先将1固定在data []中的索引0处,然后递归其余索引,然后将2固定在索引0处并递归。最后,我们修复3并递归剩余的索引。当data []中的元素数等于r(组合的大小)时,我们将打印data []。
下图显示了相同输入的递归树。
以下是上述方法的实施。
C++
// C++ program to print all combination
// of size r in an array of size n
#include
using namespace std;
void combinationUtil(int arr[], int data[],
int start, int end,
int index, int r);
// The main function that prints
// all combinations of size r
// in arr[] of size n. This function
// mainly uses combinationUtil()
void printCombination(int arr[], int n, int r)
{
// A temporary array to store
// all combination one by one
int data[r];
// Print all combination using
// temprary array 'data[]'
combinationUtil(arr, data, 0, n-1, 0, r);
}
/* arr[] ---> Input Array
data[] ---> Temporary array to
store current combination
start & end ---> Staring and
Ending indexes in arr[]
index ---> Current index in data[]
r ---> Size of a combination to be printed */
void combinationUtil(int arr[], int data[],
int start, int end,
int index, int r)
{
// Current combination is ready
// to be printed, print it
if (index == r)
{
for (int j = 0; j < r; j++)
cout << data[j] << " ";
cout << endl;
return;
}
// replace index with all possible
// elements. The condition "end-i+1 >= r-index"
// makes sure that including one element
// at index will make a combination with
// remaining elements at remaining positions
for (int i = start; i <= end &&
end - i + 1 >= r - index; i++)
{
data[index] = arr[i];
combinationUtil(arr, data, i+1,
end, index+1, r);
}
}
// Driver code
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int r = 3;
int n = sizeof(arr)/sizeof(arr[0]);
printCombination(arr, n, r);
}
// This code is contributed by rathbhupendra
C
// Program to print all combination of size r in an array of size n
#include
void combinationUtil(int arr[], int data[], int start, int end,
int index, int r);
// The main function that prints all combinations of size r
// in arr[] of size n. This function mainly uses combinationUtil()
void printCombination(int arr[], int n, int r)
{
// A temporary array to store all combination one by one
int data[r];
// Print all combination using temprary array 'data[]'
combinationUtil(arr, data, 0, n-1, 0, r);
}
/* arr[] ---> Input Array
data[] ---> Temporary array to store current combination
start & end ---> Staring and Ending indexes in arr[]
index ---> Current index in data[]
r ---> Size of a combination to be printed */
void combinationUtil(int arr[], int data[], int start, int end,
int index, int r)
{
// Current combination is ready to be printed, print it
if (index == r)
{
for (int j=0; j= r-index" makes sure that including one element
// at index will make a combination with remaining elements
// at remaining positions
for (int i=start; i<=end && end-i+1 >= r-index; i++)
{
data[index] = arr[i];
combinationUtil(arr, data, i+1, end, index+1, r);
}
}
// Driver program to test above functions
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int r = 3;
int n = sizeof(arr)/sizeof(arr[0]);
printCombination(arr, n, r);
}
Java
// Java program to print all combination of size r in an array of size n
import java.io.*;
class Combination {
/* arr[] ---> Input Array
data[] ---> Temporary array to store current combination
start & end ---> Staring and Ending indexes in arr[]
index ---> Current index in data[]
r ---> Size of a combination to be printed */
static void combinationUtil(int arr[], int data[], int start,
int end, int index, int r)
{
// Current combination is ready to be printed, print it
if (index == r)
{
for (int j=0; j= r-index" makes sure that including one element
// at index will make a combination with remaining elements
// at remaining positions
for (int i=start; i<=end && end-i+1 >= r-index; i++)
{
data[index] = arr[i];
combinationUtil(arr, data, i+1, end, index+1, r);
}
}
// The main function that prints all combinations of size r
// in arr[] of size n. This function mainly uses combinationUtil()
static void printCombination(int arr[], int n, int r)
{
// A temporary array to store all combination one by one
int data[]=new int[r];
// Print all combination using temprary array 'data[]'
combinationUtil(arr, data, 0, n-1, 0, r);
}
/*Driver function to check for above function*/
public static void main (String[] args) {
int arr[] = {1, 2, 3, 4, 5};
int r = 3;
int n = arr.length;
printCombination(arr, n, r);
}
}
/* This code is contributed by Devesh Agrawal */
Python3
# Program to print all combination
# of size r in an array of size n
# The main function that prints
# all combinations of size r in
# arr[] of size n. This function
# mainly uses combinationUtil()
def printCombination(arr, n, r):
# A temporary array to
# store all combination
# one by one
data = [0]*r;
# Print all combination
# using temprary array 'data[]'
combinationUtil(arr, data, 0,
n - 1, 0, r);
# arr[] ---> Input Array
# data[] ---> Temporary array to
# store current combination
# start & end ---> Staring and Ending
# indexes in arr[]
# index ---> Current index in data[]
# r ---> Size of a combination
# to be printed
def combinationUtil(arr, data, start,
end, index, r):
# Current combination is ready
# to be printed, print it
if (index == r):
for j in range(r):
print(data[j], end = " ");
print();
return;
# replace index with all
# possible elements. The
# condition "end-i+1 >=
# r-index" makes sure that
# including one element at
# index will make a combination
# with remaining elements at
# remaining positions
i = start;
while(i <= end and end - i + 1 >= r - index):
data[index] = arr[i];
combinationUtil(arr, data, i + 1,
end, index + 1, r);
i += 1;
# Driver Code
arr = [1, 2, 3, 4, 5];
r = 3;
n = len(arr);
printCombination(arr, n, r);
# This code is contributed by mits
C#
// C# program to print all
// combination of size r
// in an array of size n
using System;
class GFG
{
/* arr[] ---> Input Array
data[] ---> Temporary array to
store current combination
start & end ---> Staring and Ending
indexes in arr[]
index ---> Current index in data[]
r ---> Size of a combination
to be printed */
static void combinationUtil(int []arr, int []data,
int start, int end,
int index, int r)
{
// Current combination is
// ready to be printed,
// print it
if (index == r)
{
for (int j = 0; j < r; j++)
Console.Write(data[j] + " ");
Console.WriteLine("");
return;
}
// replace index with all
// possible elements. The
// condition "end-i+1 >=
// r-index" makes sure that
// including one element
// at index will make a
// combination with remaining
// elements at remaining positions
for (int i = start; i <= end &&
end - i + 1 >= r - index; i++)
{
data[index] = arr[i];
combinationUtil(arr, data, i + 1,
end, index + 1, r);
}
}
// The main function that prints
// all combinations of size r
// in arr[] of size n. This
// function mainly uses combinationUtil()
static void printCombination(int []arr,
int n, int r)
{
// A temporary array to store
// all combination one by one
int []data = new int[r];
// Print all combination
// using temprary array 'data[]'
combinationUtil(arr, data, 0,
n - 1, 0, r);
}
// Driver Code
static public void Main ()
{
int []arr = {1, 2, 3, 4, 5};
int r = 3;
int n = arr.Length;
printCombination(arr, n, r);
}
}
// This code is contributed by m_kit
PHP
Input Array
data[] ---> Temporary array to
store current combination
start & end ---> Staring and Ending
indexes in arr[]
index ---> Current index in data[]
r ---> Size of a combination
to be printed */
function combinationUtil($arr, $data, $start,
$end, $index, $r)
{
// Current combination is ready
// to be printed, print it
if ($index == $r)
{
for ($j = 0; $j < $r; $j++)
echo $data[$j];
echo "\n";
return;
}
// replace index with all
// possible elements. The
// condition "end-i+1 >=
// r-index" makes sure that
// including one element at
// index will make a combination
// with remaining elements at
// remaining positions
for ($i = $start;
$i <= $end &&
$end - $i + 1 >= $r - $index; $i++)
{
$data[$index] = $arr[$i];
combinationUtil($arr, $data, $i + 1,
$end, $index + 1, $r);
}
}
// Driver Code
$arr = array(1, 2, 3, 4, 5);
$r = 3;
$n = sizeof($arr);
printCombination($arr, $n, $r);
// This code is contributed by ajit
?>
Javascript
C++
// C++ Program to print all combination of
// size r in an array of size n
#include
using namespace std;
void combinationUtil(int arr[], int n, int r,
int index, int data[], int i);
// The main function that prints all
// combinations of size r in arr[]
// of size n. This function mainly
// uses combinationUtil()
void printCombination(int arr[], int n, int r)
{
// A temporary array to store
// all combination one by one
int data[r];
// Print all combination using
// temprary array 'data[]'
combinationUtil(arr, n, r, 0, data, 0);
}
/* arr[] ---> Input Array
n ---> Size of input array
r ---> Size of a combination to be printed
index ---> Current index in data[]
data[] ---> Temporary array to store current combination
i ---> index of current element in arr[] */
void combinationUtil(int arr[], int n, int r,
int index, int data[], int i)
{
// Current combination is ready, print it
if (index == r)
{
for (int j = 0; j < r; j++)
cout << data[j] << " ";
cout << endl;
return;
}
// When no more elements are there to put in data[]
if (i >= n)
return;
// current is included, put next at next location
data[index] = arr[i];
combinationUtil(arr, n, r, index + 1, data, i + 1);
// current is excluded, replace it with next (Note that
// i+1 is passed, but index is not changed)
combinationUtil(arr, n, r, index, data, i+1);
}
// Driver code
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int r = 3;
int n = sizeof(arr)/sizeof(arr[0]);
printCombination(arr, n, r);
return 0;
}
// This is code is contributed by rathbhupendra
C
// Program to print all combination of size r in an array of size n
#include
void combinationUtil(int arr[],int n,int r,int index,int data[],int i);
// The main function that prints all combinations of size r
// in arr[] of size n. This function mainly uses combinationUtil()
void printCombination(int arr[], int n, int r)
{
// A temporary array to store all combination one by one
int data[r];
// Print all combination using temprary array 'data[]'
combinationUtil(arr, n, r, 0, data, 0);
}
/* arr[] ---> Input Array
n ---> Size of input array
r ---> Size of a combination to be printed
index ---> Current index in data[]
data[] ---> Temporary array to store current combination
i ---> index of current element in arr[] */
void combinationUtil(int arr[], int n, int r, int index, int data[], int i)
{
// Current cobination is ready, print it
if (index == r)
{
for (int j=0; j= n)
return;
// current is included, put next at next location
data[index] = arr[i];
combinationUtil(arr, n, r, index+1, data, i+1);
// current is excluded, replace it with next (Note that
// i+1 is passed, but index is not changed)
combinationUtil(arr, n, r, index, data, i+1);
}
// Driver program to test above functions
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int r = 3;
int n = sizeof(arr)/sizeof(arr[0]);
printCombination(arr, n, r);
return 0;
}
Java
// Java program to print all combination of size r in an array of size n
import java.io.*;
class Combination {
/* arr[] ---> Input Array
data[] ---> Temporary array to store current combination
start & end ---> Staring and Ending indexes in arr[]
index ---> Current index in data[]
r ---> Size of a combination to be printed */
static void combinationUtil(int arr[], int n, int r, int index,
int data[], int i)
{
// Current combination is ready to be printed, print it
if (index == r)
{
for (int j=0; j= n)
return;
// current is included, put next at next location
data[index] = arr[i];
combinationUtil(arr, n, r, index+1, data, i+1);
// current is excluded, replace it with next (Note that
// i+1 is passed, but index is not changed)
combinationUtil(arr, n, r, index, data, i+1);
}
// The main function that prints all combinations of size r
// in arr[] of size n. This function mainly uses combinationUtil()
static void printCombination(int arr[], int n, int r)
{
// A temporary array to store all combination one by one
int data[]=new int[r];
// Print all combination using temprary array 'data[]'
combinationUtil(arr, n, r, 0, data, 0);
}
/*Driver function to check for above function*/
public static void main (String[] args) {
int arr[] = {1, 2, 3, 4, 5};
int r = 3;
int n = arr.length;
printCombination(arr, n, r);
}
}
/* This code is contributed by Devesh Agrawal */
Python 3
# Program to print all combination
# of size r in an array of size n
# The main function that prints all
# combinations of size r in arr[] of
# size n. This function mainly uses
# combinationUtil()
def printCombination(arr, n, r):
# A temporary array to store
# all combination one by one
data = [0] * r
# Print all combination using
# temprary array 'data[]'
combinationUtil(arr, n, r, 0, data, 0)
''' arr[] ---> Input Array
n ---> Size of input array
r ---> Size of a combination to be printed
index ---> Current index in data[]
data[] ---> Temporary array to store
current combination
i ---> index of current element in arr[] '''
def combinationUtil(arr, n, r, index, data, i):
# Current cobination is ready,
# print it
if (index == r):
for j in range(r):
print(data[j], end = " ")
print()
return
# When no more elements are
# there to put in data[]
if (i >= n):
return
# current is included, put
# next at next location
data[index] = arr[i]
combinationUtil(arr, n, r, index + 1,
data, i + 1)
# current is excluded, replace it
# with next (Note that i+1 is passed,
# but index is not changed)
combinationUtil(arr, n, r, index,
data, i + 1)
# Driver Code
if __name__ == "__main__":
arr = [1, 2, 3, 4, 5]
r = 3
n = len(arr)
printCombination(arr, n, r)
# This code is contributed
# by ChitraNayal
C#
// C# program to print all
// combination of size r
// in an array of size n
using System;
class GFG
{
/* arr[] ---> Input Array
data[] ---> Temporary array to
store current combination
start & end ---> Staring and Ending
indexes in arr[]
index ---> Current index in data[]
r ---> Size of a combination
to be printed */
static void combinationUtil(int []arr, int n,
int r, int index,
int []data, int i)
{
// Current combination is ready
// to be printed, print it
if (index == r)
{
for (int j = 0; j < r; j++)
Console.Write(data[j] + " ");
Console.WriteLine("");
return;
}
// When no more elements are
// there to put in data[]
if (i >= n)
return;
// current is included, put
// next at next location
data[index] = arr[i];
combinationUtil(arr, n, r,
index + 1, data, i + 1);
// current is excluded, replace
// it with next (Note that
// i+1 is passed, but index
// is not changed)
combinationUtil(arr, n, r, index,
data, i + 1);
}
// The main function that prints
// all combinations of size r
// in arr[] of size n. This
// function mainly uses combinationUtil()
static void printCombination(int []arr,
int n, int r)
{
// A temporary array to store
// all combination one by one
int []data = new int[r];
// Print all combination
// using temprary array 'data[]'
combinationUtil(arr, n, r, 0, data, 0);
}
// Driver Code
static public void Main ()
{
int []arr = {1, 2, 3, 4, 5};
int r = 3;
int n = arr.Length;
printCombination(arr, n, r);
}
}
// This code is contributed by ajit
PHP
Input Array
n ---> Size of input array
r ---> Size of a combination
to be printed
index ---> Current index in data[]
data[] ---> Temporary array to store
current combination
i ---> index of current element in arr[] */
function combinationUtil($arr, $n, $r,
$index, $data, $i)
{
// Current cobination
// is ready, print it
if ($index == $r)
{
for ($j = 0; $j < $r; $j++)
echo $data[$j], " ";
echo "\n";
return;
}
// When no more elements are
// there to put in data[]
if ($i >= $n)
return;
// current is included, put
// next at next location
$data[$index] = $arr[$i];
combinationUtil($arr, $n, $r,
$index + 1,
$data, $i + 1);
// current is excluded, replace
// it with next (Note that i+1
// is passed, but index is not changed)
combinationUtil($arr, $n, $r,
$index, $data, $i + 1);
}
// Driver Code
$arr = array(1, 2, 3, 4, 5);
$r = 3;
$n = sizeof($arr);
printCombination($arr, $n, $r);
// This code is contributed by ajit
?>
Javascript
输出:
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
如何处理重复项?
请注意,上述方法不处理重复项。例如,如果输入数组为{1、2、1},而r为2,则程序将{1、2}和{2、1}打印为两个不同的组合。我们可以通过在上面的代码中添加以下两点来避免重复。
1)在printCombination()中调用combinationUtil()之前,添加代码以对数组进行排序
2)在combinationUtil()的for循环末尾添加以下行
// Since the elements are sorted, all occurrences of an element
// must be together
while (arr[i] == arr[i+1])
i++;
该手柄重复实现看到这一点。
方法2(包括和排除每个元素)
与上述方法一样,我们创建一个临时数组data []。这里的想法类似于子集和问题。我们一个接一个地考虑输入数组的每个元素,然后重复两种情况:
1)元素包含在当前组合中(我们将元素放入data []中,并在data []中增加下一个可用索引)
2)当前组合中不包含该元素(我们不放置该元素并且不更改索引)
当data []中的元素数等于r(组合的大小)时,我们将其打印出来。
该方法主要基于Pascal的Identity,即n cr = n-1 cr + n-1 cr-1
以下是方法2的实现。
C++
// C++ Program to print all combination of
// size r in an array of size n
#include
using namespace std;
void combinationUtil(int arr[], int n, int r,
int index, int data[], int i);
// The main function that prints all
// combinations of size r in arr[]
// of size n. This function mainly
// uses combinationUtil()
void printCombination(int arr[], int n, int r)
{
// A temporary array to store
// all combination one by one
int data[r];
// Print all combination using
// temprary array 'data[]'
combinationUtil(arr, n, r, 0, data, 0);
}
/* arr[] ---> Input Array
n ---> Size of input array
r ---> Size of a combination to be printed
index ---> Current index in data[]
data[] ---> Temporary array to store current combination
i ---> index of current element in arr[] */
void combinationUtil(int arr[], int n, int r,
int index, int data[], int i)
{
// Current combination is ready, print it
if (index == r)
{
for (int j = 0; j < r; j++)
cout << data[j] << " ";
cout << endl;
return;
}
// When no more elements are there to put in data[]
if (i >= n)
return;
// current is included, put next at next location
data[index] = arr[i];
combinationUtil(arr, n, r, index + 1, data, i + 1);
// current is excluded, replace it with next (Note that
// i+1 is passed, but index is not changed)
combinationUtil(arr, n, r, index, data, i+1);
}
// Driver code
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int r = 3;
int n = sizeof(arr)/sizeof(arr[0]);
printCombination(arr, n, r);
return 0;
}
// This is code is contributed by rathbhupendra
C
// Program to print all combination of size r in an array of size n
#include
void combinationUtil(int arr[],int n,int r,int index,int data[],int i);
// The main function that prints all combinations of size r
// in arr[] of size n. This function mainly uses combinationUtil()
void printCombination(int arr[], int n, int r)
{
// A temporary array to store all combination one by one
int data[r];
// Print all combination using temprary array 'data[]'
combinationUtil(arr, n, r, 0, data, 0);
}
/* arr[] ---> Input Array
n ---> Size of input array
r ---> Size of a combination to be printed
index ---> Current index in data[]
data[] ---> Temporary array to store current combination
i ---> index of current element in arr[] */
void combinationUtil(int arr[], int n, int r, int index, int data[], int i)
{
// Current cobination is ready, print it
if (index == r)
{
for (int j=0; j= n)
return;
// current is included, put next at next location
data[index] = arr[i];
combinationUtil(arr, n, r, index+1, data, i+1);
// current is excluded, replace it with next (Note that
// i+1 is passed, but index is not changed)
combinationUtil(arr, n, r, index, data, i+1);
}
// Driver program to test above functions
int main()
{
int arr[] = {1, 2, 3, 4, 5};
int r = 3;
int n = sizeof(arr)/sizeof(arr[0]);
printCombination(arr, n, r);
return 0;
}
Java
// Java program to print all combination of size r in an array of size n
import java.io.*;
class Combination {
/* arr[] ---> Input Array
data[] ---> Temporary array to store current combination
start & end ---> Staring and Ending indexes in arr[]
index ---> Current index in data[]
r ---> Size of a combination to be printed */
static void combinationUtil(int arr[], int n, int r, int index,
int data[], int i)
{
// Current combination is ready to be printed, print it
if (index == r)
{
for (int j=0; j= n)
return;
// current is included, put next at next location
data[index] = arr[i];
combinationUtil(arr, n, r, index+1, data, i+1);
// current is excluded, replace it with next (Note that
// i+1 is passed, but index is not changed)
combinationUtil(arr, n, r, index, data, i+1);
}
// The main function that prints all combinations of size r
// in arr[] of size n. This function mainly uses combinationUtil()
static void printCombination(int arr[], int n, int r)
{
// A temporary array to store all combination one by one
int data[]=new int[r];
// Print all combination using temprary array 'data[]'
combinationUtil(arr, n, r, 0, data, 0);
}
/*Driver function to check for above function*/
public static void main (String[] args) {
int arr[] = {1, 2, 3, 4, 5};
int r = 3;
int n = arr.length;
printCombination(arr, n, r);
}
}
/* This code is contributed by Devesh Agrawal */
的Python 3
# Program to print all combination
# of size r in an array of size n
# The main function that prints all
# combinations of size r in arr[] of
# size n. This function mainly uses
# combinationUtil()
def printCombination(arr, n, r):
# A temporary array to store
# all combination one by one
data = [0] * r
# Print all combination using
# temprary array 'data[]'
combinationUtil(arr, n, r, 0, data, 0)
''' arr[] ---> Input Array
n ---> Size of input array
r ---> Size of a combination to be printed
index ---> Current index in data[]
data[] ---> Temporary array to store
current combination
i ---> index of current element in arr[] '''
def combinationUtil(arr, n, r, index, data, i):
# Current cobination is ready,
# print it
if (index == r):
for j in range(r):
print(data[j], end = " ")
print()
return
# When no more elements are
# there to put in data[]
if (i >= n):
return
# current is included, put
# next at next location
data[index] = arr[i]
combinationUtil(arr, n, r, index + 1,
data, i + 1)
# current is excluded, replace it
# with next (Note that i+1 is passed,
# but index is not changed)
combinationUtil(arr, n, r, index,
data, i + 1)
# Driver Code
if __name__ == "__main__":
arr = [1, 2, 3, 4, 5]
r = 3
n = len(arr)
printCombination(arr, n, r)
# This code is contributed
# by ChitraNayal
C#
// C# program to print all
// combination of size r
// in an array of size n
using System;
class GFG
{
/* arr[] ---> Input Array
data[] ---> Temporary array to
store current combination
start & end ---> Staring and Ending
indexes in arr[]
index ---> Current index in data[]
r ---> Size of a combination
to be printed */
static void combinationUtil(int []arr, int n,
int r, int index,
int []data, int i)
{
// Current combination is ready
// to be printed, print it
if (index == r)
{
for (int j = 0; j < r; j++)
Console.Write(data[j] + " ");
Console.WriteLine("");
return;
}
// When no more elements are
// there to put in data[]
if (i >= n)
return;
// current is included, put
// next at next location
data[index] = arr[i];
combinationUtil(arr, n, r,
index + 1, data, i + 1);
// current is excluded, replace
// it with next (Note that
// i+1 is passed, but index
// is not changed)
combinationUtil(arr, n, r, index,
data, i + 1);
}
// The main function that prints
// all combinations of size r
// in arr[] of size n. This
// function mainly uses combinationUtil()
static void printCombination(int []arr,
int n, int r)
{
// A temporary array to store
// all combination one by one
int []data = new int[r];
// Print all combination
// using temprary array 'data[]'
combinationUtil(arr, n, r, 0, data, 0);
}
// Driver Code
static public void Main ()
{
int []arr = {1, 2, 3, 4, 5};
int r = 3;
int n = arr.Length;
printCombination(arr, n, r);
}
}
// This code is contributed by ajit
的PHP
Input Array
n ---> Size of input array
r ---> Size of a combination
to be printed
index ---> Current index in data[]
data[] ---> Temporary array to store
current combination
i ---> index of current element in arr[] */
function combinationUtil($arr, $n, $r,
$index, $data, $i)
{
// Current cobination
// is ready, print it
if ($index == $r)
{
for ($j = 0; $j < $r; $j++)
echo $data[$j], " ";
echo "\n";
return;
}
// When no more elements are
// there to put in data[]
if ($i >= $n)
return;
// current is included, put
// next at next location
$data[$index] = $arr[$i];
combinationUtil($arr, $n, $r,
$index + 1,
$data, $i + 1);
// current is excluded, replace
// it with next (Note that i+1
// is passed, but index is not changed)
combinationUtil($arr, $n, $r,
$index, $data, $i + 1);
}
// Driver Code
$arr = array(1, 2, 3, 4, 5);
$r = 3;
$n = sizeof($arr);
printCombination($arr, $n, $r);
// This code is contributed by ajit
?>
Java脚本
输出 :
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
如何处理方法2中的重复项?
像方法1一样,我们可以遵循两件事来处理重复项。
1)在printCombination()中调用combinationUtil()之前,添加代码以对数组进行排序
2)在combinationUtil()的combinationUtil()的两个递归调用之间添加以下行
// Since the elements are sorted, all occurrences of an element
// must be together
while (arr[i] == arr[i+1])
i++;