F



i



v



e



-



gr



ea



t
蓝桥杯和为T(动态规划)

问题描述
  从一个大小为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">&amp;&amp;</span>pos<span class="token operator">!=</span><span class="token number">0</span><span class="token operator">&amp;&amp;</span>i<span class="token operator">&lt;</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token comment" spellcheck="true">//&#x7EC8;&#x6B62;&#x6761;&#x4EF6; &#x6CE8;&#x610F;&#x5168;&#x90E8;&#x904D;&#x5386;&#x5B8C;&#x518D;&#x8FDB;&#x884C;&#x5224;&#x65AD;(&#x9632;&#x6B62; -1 1 -2 2 3 -3&#x7684;&#x7279;&#x4F8B;) &#x5E76;&#x4E14; &#x4E0D;&#x80FD;&#x4E0D;&#x9009; </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">&gt;=</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">//&#x5012;&#x7740;&#x904D;&#x5386;&#x5012;&#x7740;&#x8F93;&#x51FA; </span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%ld &quot;</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">&quot;\n&quot;</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">&lt;</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">//&#x904D;&#x5386;&#x5230;&#x6570;&#x7EC4;&#x7B2C;&#x4E00;&#x9879;&#x8FD8;&#x672A;&#x6EE1;&#x8DB3;t==T;&#x5219;&#x8FD4;&#x56DE; </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;
}