给定一个整数数组,打印其中的所有子集的总和。输出总和可以以任何顺序打印。
例子 :
Input : arr[] = {2, 3}
Output: 0 2 3 5
Input : arr[] = {2, 4, 5}
Output : 0 2 4 5 6 7 9 11
方法1(递归)
我们可以递归地解决这个问题。总共有2 n个子集。对于每个元素,我们考虑两个选择,我们将其包含在子集中,而没有将其包含在子集中。以下是基于此思想的递归解决方案。
C++
// C++ program to print sums of all possible
// subsets.
#include
using namespace std;
// Prints sums of all subsets of arr[l..r]
void subsetSums(int arr[], int l, int r,
int sum=0)
{
// Print current subset
if (l > r)
{
cout << sum << " ";
return;
}
// Subset including arr[l]
subsetSums(arr, l+1, r, sum+arr[l]);
// Subset excluding arr[l]
subsetSums(arr, l+1, r, sum);
}
// Driver code
int main()
{
int arr[] = {5, 4, 3};
int n = sizeof(arr)/sizeof(arr[0]);
subsetSums(arr, 0, n-1);
return 0;
}
Java
// Java program to print sums
// of all possible subsets.
import java .io.*;
class GFG
{
// Prints sums of all
// subsets of arr[l..r]
static void subsetSums(int []arr, int l,
int r, int sum )
{
// Print current subset
if (l > r)
{
System.out.print(sum + " ");
return;
}
// Subset including arr[l]
subsetSums(arr, l + 1, r,
sum + arr[l]);
// Subset excluding arr[l]
subsetSums(arr, l + 1, r, sum);
}
// Driver code
public static void main (String[] args)
{
int []arr = {5, 4, 3};
int n = arr.length;
subsetSums(arr, 0, n - 1, 0);
}
}
// This code is contributed by anuj_67
Python3
# Python3 program to print sums of
# all possible subsets.
# Prints sums of all subsets of arr[l..r]
def subsetSums(arr, l, r, sum = 0):
# Print current subset
if l > r:
print (sum, end = " ")
return
# Subset including arr[l]
subsetSums(arr, l + 1, r, sum + arr[l])
# Subset excluding arr[l]
subsetSums(arr, l + 1, r, sum)
# Driver code
arr = [5, 4, 3]
n = len(arr)
subsetSums(arr, 0, n - 1)
# This code is contributed by Shreyanshi Arun.
C#
// C# program to print sums of all possible
// subsets.
using System;
class GFG {
// Prints sums of all subsets of
// arr[l..r]
static void subsetSums(int []arr, int l,
int r, int sum )
{
// Print current subset
if (l > r)
{
Console.Write(sum + " ");
return;
}
// Subset including arr[l]
subsetSums(arr, l+1, r, sum + arr[l]);
// Subset excluding arr[l]
subsetSums(arr, l+1, r, sum);
}
// Driver code
public static void Main ()
{
int []arr = {5, 4, 3};
int n = arr.Length;
subsetSums(arr, 0, n-1,0);
}
}
// This code is contributed by anuj_67
PHP
$r)
{
echo $sum , " ";
return;
}
// Subset including arr[l]
subsetSums($arr, $l + 1, $r,
$sum + $arr[$l]);
// Subset excluding arr[l]
subsetSums($arr, $l + 1, $r, $sum);
}
// Driver code
$arr = array(5, 4, 3);
$n = count($arr);
subsetSums($arr, 0, $n - 1);
// This code is contributed by anuj_67.
?>
Javascript
C++
// Iterative C++ program to print sums of all
// possible subsets.
#include
using namespace std;
// Prints sums of all subsets of array
void subsetSums(int arr[], int n)
{
// There are totoal 2^n subsets
long long total = 1<
Java
// Iterative Java program to print sums of all
// possible subsets.
import java.util.*;
class GFG{
// Prints sums of all subsets of array
static void subsetSums(int arr[], int n)
{
// There are totoal 2^n subsets
int total = 1 << n;
// Consider all numbers from 0 to 2^n - 1
for(int i = 0; i < total; i++)
{
int sum = 0;
// Consider binary reprsentation of
// current i to decide which elements
// to pick.
for(int j = 0; j < n; j++)
if ((i & (1 << j)) != 0)
sum += arr[j];
// Print sum of picked elements.
System.out.print(sum + " ");
}
}
// Driver code
public static void main(String args[])
{
int arr[] = new int[]{ 5, 4, 3 };
int n = arr.length;
subsetSums(arr, n);
}
}
// This code is contributed by spp____
PHP
Javascript
输出 :
12 9 8 5 7 4 3 0
方法2(迭代)
如上所述,总共有2 n个子集。这个想法是生成从0到2 n – 1的循环。对于每个数字,请选择与当前数字的二进制表示形式中的1s对应的所有数组元素。
C++
// Iterative C++ program to print sums of all
// possible subsets.
#include
using namespace std;
// Prints sums of all subsets of array
void subsetSums(int arr[], int n)
{
// There are totoal 2^n subsets
long long total = 1<
Java
// Iterative Java program to print sums of all
// possible subsets.
import java.util.*;
class GFG{
// Prints sums of all subsets of array
static void subsetSums(int arr[], int n)
{
// There are totoal 2^n subsets
int total = 1 << n;
// Consider all numbers from 0 to 2^n - 1
for(int i = 0; i < total; i++)
{
int sum = 0;
// Consider binary reprsentation of
// current i to decide which elements
// to pick.
for(int j = 0; j < n; j++)
if ((i & (1 << j)) != 0)
sum += arr[j];
// Print sum of picked elements.
System.out.print(sum + " ");
}
}
// Driver code
public static void main(String args[])
{
int arr[] = new int[]{ 5, 4, 3 };
int n = arr.length;
subsetSums(arr, n);
}
}
// This code is contributed by spp____
的PHP
Java脚本
输出 :
0 5 4 9 3 8 7 12
感谢cfh在评论中建议上述迭代解决方案。
注意:我们实际上并没有创建子集来找到它们的总和,而是只是使用了递归来找到给定集合的非连续子集的总和。