原文:
用不同的元素生成给定数组的所有可能的大小子集。
示例:
input : arr[] = {1, 2, 3, 4}
r = 2
output : 1 2
1 3
1 4
2 3
2 4
3 4
input : arr[] = {10, 20, 30, 40, 50}
r = 3
output : 10 20 30
10 20 40
10 20 50
10 30 40
10 30 50
10 40 50
20 30 40
20 30 50
20 40 50
30 40 50
这个问题是一样的。 这里的思路类似于。我们逐一考虑输入数组的每一个元素,并针对两种情况重复出现: 1)元素包含在当前组合中(我们将元素放入 data【】中,并增加 data【】中的下一个可用索引) 2)元素排除在当前组合中(我们不放入元素,也不改变索引) 当 data【】中的元素数量变为等于 r(组合的大小)时,我们打印它。 该方法主要基于帕斯卡恒等式,即ncr= n-1cr n-1cr-1t16】
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 temporary 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 <<"\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 program to test above functions
int main()
{
int arr[] = { 10, 20, 30, 40, 50 };
int r = 3;
int n = sizeof(arr) / sizeof(arr[0]);
printcombination(arr, n, r);
return 0;
}
// this code is contributed by shivanisinghss2110
c
// 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 temporary 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 )
printf("%d ", data[j]);
printf("\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 program to test above functions
int main()
{
int arr[] = { 10, 20, 30, 40, 50 };
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 permutation {
/* arr[] ---> input array
data[] ---> temporary array to store current combination
start & end ---> starting 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 )
system.out.print(data[j] " ");
system.out.println("");
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 temporary
// array 'data[]'
combinationutil(arr, n, r, 0, data, 0);
}
/* driver function to check for above function */
public static void main(string[] args)
{
int arr[] = { 10, 20, 30, 40, 50 };
int r = 3;
int n = arr.length;
printcombination(arr, n, r);
}
}
/* this code is contributed by devesh agrawal */
python 3
# python3 program to print all
# subset combination of n
# element in given set of r element .
# arr[] ---> input array
# data[] ---> temporary array to
# store current combination
# start & end ---> starting and ending
# indexes in arr[]
# index ---> current index in data[]
# r ---> size of a combination
# to be printed
def combinationutil(arr, n, r,
index, data, i):
# current combination is
# ready to be printed,
# 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)
# 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 = list(range(r))
# print all combination
# using temporary
# array 'data[]'
combinationutil(arr, n, r,
0, data, 0)
# driver code
arr = [10, 20, 30, 40, 50]
r = 3
n = len(arr)
printcombination(arr, n, r)
# this code is contributed
# by ambuj sahu
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 --->
starting 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
// temporary array 'data[]'
combinationutil(arr, n, r, 0, data, 0);
}
/* driver function to check for
above function */
public static void main()
{
int []arr = { 10, 20, 30, 40, 50 };
int r = 3;
int n = arr.length;
printcombination(arr, n, r);
}
}
// this code is contributed by vt_m.
服务器端编程语言(professional hypertext preprocessor 的缩写)
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 combination 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 program to test above functions
$arr = array( 10, 20, 30, 40, 50 );
$r = 3;
$n = count($arr);
printcombination($arr, $n, $r);
// this code is contributed by anuj_67.
?>
java 描述语言
输出:
10 20 30
10 20 40
10 20 50
10 30 40
10 30 50
10 40 50
20 30 40
20 30 50
20 40 50
30 40 50
关于处理输入数组中重复项的更多pg电子试玩链接的解决方案和想法,请参考下面的帖子。 。
本文由 dhiman mayank 供稿。如果你喜欢 geeksforgeeks 并想投稿,你也可以使用写一篇文章或者把你的文章邮寄到 review-team@geeksforgeeks.org。看到你的文章出现在极客博客pg电子试玩链接主页上,帮助其他极客。 如果你发现任何不正确的地方,或者你想分享更多关于上面讨论的话题的信息,请写评论。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处