代码:
#include <iostream>
using namespace std;
long long a[35];
int vis[35], N, T, count = 0;
void F(int n)
{
int i, sum = 0, vsum = 0;
if (n > 0)
{
for (i = 0 ; i < 2 ; i++)//0->不取该数,1->取该数
{
vis[n - 1] = i;
F(n - 1);//对第n - 1个数进行遍历
}
}
else
{
for (i = 0 ; i < N ; i++)
{
if (vis[i])
{
sum += a[i];
vsum += vis[i];//必须取数
}
}
if (sum == T && vsum)
{
count++;//成立的次数
for (i = 0 ; i < N ; i++)
{
if (vis[i])
{
cout << a[i] << " ";
}
}
cout << endl;
}
}
}
int main()
{
int n, i, t;
cin >> n;
N = n;
for (i = 0 ; i < n ; i++)
{
cin >> a[i];
}
cin >> t;
T = t;
F(n);
cout << count;
return 0;
}