I learned this before when I thought about greedy algorithms. In fact, the application of greedy algorithms is a prerequisite. Under the current situation, for example, the values of common 1, 2, and 5, 10 are carefully designed. In fact, most of them should use dynamic planning. Of course, this article only uses search. For details, refer to the following connection.
For example, values 1 2 and 2 1 are the same. In integer decomposition, the array is incremented. For example, 10 is used to break down. In reality, values after 1 2 are greater than or equal to the preceding values, A coin is not an integer decomposition.
# Include <stdio. h>
# Include <iostream>
# Include <vector>
Using namespace std;
Int count (int S [], int m, int n)
{
// If n is 0, a solution is found.
If (n = 0)
Return 1;
If (n <0)
Return 0;
// If no coin is available, 0 is returned.
If (m <= 0)
Return 0;
// Follow the above Recursive Function
Return count (S, m-1, n) + count (S, m, n-S [s-1]);
}
Int count1 (int S [], int len, int left, int ans [], int Levey) // use ans Levey to obtain the answer. You can use vertor.
{
If (left <0) return 0;
If (left = 0)
{
Return 1;
}
Int sum = 0;
For (int k = 0; k <len; k ++)
{
If (ans [lev] <= S [k])
{
Ans [lev+ 1] = S [k];
Int a = count1 (S, len, left-S [k], ans, lev+ 1 );
// Cout <a <endl;
Sum + =;
}
}
Return sum;
}
// Test
Int main ()
{
Int I, j;
Int arr [] = {1, 2, 3, 5, 6, 7, 8, 9 };
Int m = sizeof (arr)/sizeof (arr [0]);
Printf ("% d", count (arr, m, 14 ));
Vector <int> v;
Int * ans = new int [20];
Ans [0] =-1;
Cout <"my code:" <count1 (arr, m, 14, ans, 0) <endl;
Getchar ();
Return 0;
}
# Include <stdio. h >#include <iostream >#include <vector> using namespace std; int count (int S [], int m, int n) {// if n is 0, A solution is found: if (n = 0) return 1; if (n <0) return 0; // No coins are available, also return 0 if (m <= 0) return 0; // according to the above recursive function return count (S, m-1, n) + count (S, m, n-S [M-1]);} int count1 (int S [], int len, int left, int ans [], int Levey) // use it with ans Levey, to obtain the answer, use vertor {if (left <0) return 0; if (left = 0) {return 1 ;}int sum = 0; for (int k = 0; k <len; k ++) {if (ans [Ev] <= S [k]) {ans [lev+ 1] = S [k]; int a = count1 (S, len, left-S [k], ans, lev+ 1); // cout <a <endl; sum + = ;}} return sum;} // test int main () {int I, j; int arr [] = {1, 2, 3, 5, 6, 8, 9 }; int m = sizeof (arr)/sizeof (arr [0]); printf ("% d", count (arr, m, 14); vector <int> v; int * ans = new int [20]; ans [0] =-1; cout <"my code:" <count1 (arr, m, 14, ans, 0) <endl; getchar (); return 0 ;}