问题描述
从一个大小为n的整数集中选取一些元素,使得它们的和等于给定的值T。
每个元素限选一次,不能一个都不选。
输入格式
第一行一个正整数n,表示整数集内元素的个数。
第二行n个整数,用空格隔开。
第三行一个整数T,表示要达到的和。
输出格式
输出有若干行,每行输出一组解,即所选取的数字,按照输入中的顺序排列。
若有多组解,优先输出不包含第n个整数的;若都包含或都不包含,
优先输出不包含第n-1个整数的,依次类推。
最后一行输出总方案数。
样例输入
5
-7 -3 -2 5 9
0
样例输出
-3 -2 5
-7 -2 9
2
数据规模和约定
1<=n<=22
T<=maxlongint
集合中任意元素的和都不超过long的范围
思路:都遍历一遍 ;对于数组里的值 只有两种情况 要么取 要么不取 通过回溯 法遍历每一种情况
根据要求 预先不考虑 数组后面 的数字 因此需倒着遍历并且先遍历不取再遍历取的情况
#include <stdio.h>
long int a[30];
long int b[30];
long int T;
int n;
long int sum=0;
void f(int long t,int pos,int i )
{int j;
<span class="token keyword">if</span><span class="token punctuation">(</span>t<span class="token operator">==</span>T<span class="token operator">&&</span>pos<span class="token operator">!=</span><span class="token number">0</span><span class="token operator">&&</span>i<span class="token operator"><</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">//终止条件 注意全部遍历完再进行判断(防止 -1 1 -2 2 3 -3的特例) 并且 不能不选 </span>
<span class="token punctuation">{</span>sum<span class="token operator">++</span><span class="token punctuation">;</span>
<span class="token keyword">for</span><span class="token punctuation">(</span>j<span class="token operator">=</span>pos<span class="token number">-1</span><span class="token punctuation">;</span>j<span class="token operator">>=</span><span class="token number">0</span><span class="token punctuation">;</span>j<span class="token operator">--</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">//倒着遍历倒着输出 </span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"%ld "</span><span class="token punctuation">,</span>b<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">"\n"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">else</span>
<span class="token punctuation">{</span> <span class="token keyword">if</span><span class="token punctuation">(</span>i<span class="token operator"><</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token keyword">return</span> <span class="token punctuation">;</span><span class="token comment" spellcheck="true">//遍历到数组第一项还未满足t==T;则返回 </span>
<span class="token function">f</span><span class="token punctuation">(</span>t<span class="token punctuation">,</span>pos<span class="token punctuation">,</span>i<span class="token number">-1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
t<span class="token operator">+</span><span class="token operator">=</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>b<span class="token punctuation">[</span>pos<span class="token punctuation">]</span><span class="token operator">=</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token function">f</span><span class="token punctuation">(</span>t<span class="token punctuation">,</span>pos<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">,</span>i<span class="token number">-1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
t<span class="token operator">-</span><span class="token operator">=</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
}
int main()
{
int i;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%ld",&a[i]);
scanf("%ld",&T);
f(0,0,n);
printf("%d\n",sum);
return 0;
}