1 // Method a: the number of recursive items and item volume n array a [100] to global variables;
2 // COUNT (i, SUM) represents the number of combinations from the start of the i-th array back to statistics and sum of the number of types,
. 3 // sum and number combination is: COUT (I, sum) = COUT (I +. 1, sum-a [I]) + COUT (I +. 1, sum),
. 4 // wherein cout (i + 1, sum- a [i]) represents contains a [i], that is, from the first i + 1 next count number
5 // number and combination of sum-a [i] is the type number, and cout (i + 1, sum) indicates not comprise a [i], that is, from the number i + 1 onward statistical combination of numbers and the number of types of ******** sum ***************************
. 6 #include <the iostream>
. 7 the using namespace STD;
. 8
. 9 int A [ 100 ];
10 int n-= . 1 ;
. 11 int COUNT (int I, int sum) {
12 is IF (sum == 0 ) {
13 is return . 1 ; // find a set of sum and number combination;
14 }
15 IF (I == n-sum || < 0 ) return 0 ; // I == no other n-number of instructions are combined, sum <0 indicate that a combination not;
16 return COUNT (I + . 1 , a-SUM [I]) + COUNT (I + . 1 , SUM); // from the array i is first started, comprising A [i], and does not include;
. 17 }
18 is
. 19 int main () {
20 is the while (CIN >> n-) {
21 is for(int i=0; i<n; i++)
22 cin>>a[i];
23 cout<<count(0,40)<<endl;
24 }
25 return 0;
26 }
1 // Method two: dynamic programming
2 #include <the iostream>
. 3 the using namespace STD;
. 4 #define N 100
. 5 int n-, A [N];
. 6 int main () {
. 7 the while (CIN >> n-) {
. 8 int (DP *) [ 50 ] = new new int [N] [ 50 ]; // DP [i] [J] indicates the i-th item in the front volume Couchu J;
. 9 for ( int i = . 1 ; i <= n-; ++ I ) {
10 CIN >> A [I];
. 11 DP [I] [0]=1; //初始边界
12 }
13 dp[0][0]=1;
14 for(int i=1; i<=n; i++)
15 for(int j=1; j<=40; j++) {
16 dp[i][j]=dp[i-1][j];
17 if(a[i]<=j)
18 dp[i][j]+=dp[i-1][j-a[i]];
19 }
20 cout<<dp[n][40]<<endl;
21 delete []dp;
22 }
23 return 0;
24 }