C ++ handles a dynamic planning problem and Dynamic Planning
Well, I was dizzy when I looked at the questions asked by others. Baidu made a dynamic plan and thought about how to do it. Today, I wrote code and implemented it ~
The requirement is recursion and dynamic planning. If you think about this method, it is also the simplest ~
The so-called dynamic planning: the multi-stage process is transformed into a series of single-stage problems, and the relationship between each stage is used to solve them one by one. A dynamic planning algorithm is usually used to solve a problem with an optimal nature. In such problems, there may be many feasible solutions. Each solution corresponds to a value. We hope to find a solution with the optimal value. The dynamic planning algorithm is similar to the divide and conquer method. Its basic idea is to break down the problem to be solved into several subproblems, first solve the subproblems, and then obtain the original solution from the subproblems. Different from the Division and control method, it is suitable for Dynamic Planning and solving problems. After decomposition, subproblems are often not independent of each other. If you use the divide and conquer method to solve this type of problem, the number of subproblems obtained by the decomposition is too large, and some subproblems are repeatedly calculated many times. If we can save the answers to the resolved sub-questions and find the answers we have obtained when necessary, we can avoid a large number of repeated computations and save time. We can use a table to record the answers to all resolved subquestions. Whether or not this subproblem is used in the future, as long as it has been computed, the results will be filled in the table. This is the basic idea of dynamic planning. (From encyclopedia) (time complexity is the complexity of a polynomial)
The Knapsack problem (Knapsack problem) is a complete NP problem of combined optimization. The problem can be described as: Given a group of items, each item has its own weight and price. Within the limited total weight, how can we choose to make the total price of the item the highest. The name of the question comes from how to choose the most suitable item to place in the given backpack.
Example:
Solution:
N represents the nominal value
If n is 1, check whether division can be performed;
N> 1, look at the number of times when there is no nth nominal value, and then see 1 or 2 .... when j/n faces are n, several coins are required to obtain the minimum value.
Directly exist in the array, and then go to the minimum value in the array.
Code:
1 #include"header_file.h" 2 using namespace std; 3 4 int coin_num(vector<int> T,int i,int j) 5 { 6 if(i==1) 7 { 8 if(j%T[0]==0) 9 {10 return j/T[0];11 }12 else13 {14 return 9999;15 }16 }17 else18 {19 int min;20 min=coin_num(T,i-1,j);21 int temp;22 temp=j/T[i-1];23 for(int m=0;m<=temp;m++)24 {25 if(min>(m+coin_num(T,i-1,j-m*T[i-1])))26 min=m+coin_num(T,i-1,j-m*T[i-1]);27 28 }29 return min;30 }31 }32 33 vector<int> all_num(vector<int> T,int j)34 {35 vector<int> v;36 for(int i=0;i<T.size();i++)37 v.push_back(coin_num(T,i+1,j));38 // for(int i=0;i<T.size();i++) //use for test39 // cout<<v[i]<<" ";40 //v.push_back(coin_num(T,i+1,j));41 return v;42 }43 44 int find_min(vector<int> v)45 {46 int min=0;47 for(int i=1;i<v.size();i++)48 {49 if(v[min]>v[i])50 min=i;51 }52 return v[min];53 }54 55 int main(void)56 {57 int n;58 cout<<"input n:";59 cin>>n;60 61 vector<int> T;62 for(int i=0;i<n;i++)63 {64 int temp;65 cin>>temp;66 T.push_back(temp);67 }68 69 int j;70 cout<<"input j:";71 cin>>j;72 73 vector<int> v;74 v=all_num(T,j);75 int min;76 min=find_min(v);77 cout<<"min:"<<min<<endl;78 }