Programming Algorithm, programming algorithm manual for beginners
Continuous and maximum sub-array code (C)
Address: http://blog.csdn.net/caroline_wendy
Question: In an array, find the continuous and maximum subsequences.
Use two variables, one to store the current value, one to store the maximum value, and a temporary array to update the maximum and array.
Time complexity O (n ).
Code:
/* * main.cpp * * Created on: 2014.9.19 * Author: spike */#include <iostream>#include <vector>#include <climits>using namespace std;int FindSequence (int data[], int length, vector<int>& vi){if (data == NULL || length <= 0)return INT_MIN;int greatest = INT_MIN;int cur = 0;vector<int> tmp;for (int i=0; i<length; ++i) {if (cur <= 0) {cur = data[i];tmp.clear();tmp.push_back(data[i]);} else {cur += data[i];tmp.push_back(data[i]);}if (cur > greatest) {vi = tmp;greatest = cur;}}return greatest;}int main(void){int data[] = {1, -2, 3, 10, -4, 7, 2, -5};int length = sizeof(data)/sizeof(data[0]);vector<int> vi;int g = FindSequence(data, length, vi);cout << g << ": ";for (size_t i=0; i<vi.size(); ++i) {cout << vi[i] << " ";}cout << endl; return 0;}
Output:
18: 3 10 -4 7 2
Programming Algorithm Problems
Typical problems with Dynamic Planning of backpacks
1 search method: exhaustive query of all possible fetch Conditions
2. Dynamic Planning
Are you a newbie? Because the data size is very small, you can simply use the search method. It is easier to write programs because it is easier to understand the data ~~
You can find out about your backpack and provide detailed answers everywhere ~~~
Zhidao.baidu.com/question/25002746.html
Similar to this problem, you only need to change the question and code.
After the change, the idea is as follows to help you replace it with the C language ~~~~
There is a box with a capacity of v (positive integer, o ≤ v ≤ 20000) and n items (o ≤ n ≤ 30). Each item has a volume (positive integer ). From n items, if thousands of items are loaded into the box, the remaining space of the box must be zero. (Here v is your s, and here n is your n)
L search method
Void search (int k, int v) {searches for the k-th item, and the remaining space is v}
{
Int I, j;
If (v <best) best = v;
If v-(s [n]-s [k-1])> = best return; // s [n] indicates the weight and
If (k <= n ){
If (v> w [k]) search (k + 1, v-w [k]); // w [n] indicates the weight of the nth item
Search (k + 1, v );
}
}
Best is the global variable, indicating the minimum value of the remaining space of the box. It is good to set the initial value to a very large positive number.
Therefore, if the best value is 0 after search (n, v), it indicates that there is a solution.
2 DP Dynamic Planning (iterative method)
F [I, j] is a boolean type that selects a number of signs from the first I items to make the size exactly j.
Implementation: converts optimization problems into judgment problems
F [I] [j] = (F [I-1] [j-w [I] | F [I-1] [j]) (w [I] <= j <= v) Boundary: f [0] [0] = true;
For (I = 1; I <= n; I ++)
For (j = w [I]; j> = v; j --)
F [I] [j] = (F [I-1] [j-w [I] | F [I-1] [j]);
After calculation, if F [n] [v] is true, there is a solution.
3 DP Dynamic Planning (Iterative Optimization)
Optimization: the current status is only related to the status of the previous stage and can be reduced to one dimension.
F [0] = true;
For (I = 1; I <= n; I ++)
For (j = w [I]; j> = v; j --)
F [j] = (F [j-w [I] | F [j]);
After calculation, if F [v] is true, there is a solution.
4 DP Dynamic Planning (memorandum algorithm)
F is a two-dimensional array. int F [n] [v] is initialized to 0 at the beginning.
Bool dp (int n, int v)
{
If (n = 0 & v = 0) return 1;
If (n = 0 & v! = 0) return-1;
If (F [n] [v]! = 0) return F [n] [v];
If (dp (n-1, v-w [n]) return F [n] [v] = 1;
If (v> = w [n] & dp (n-1, v-w [n]) return F [n] [v] = 1;
Return F [n] [v] =-1;
} ...... Remaining full text>
What is the use of programming algorithms?
Studying other people's algorithms allows you to think on the shoulders of giants.
In fact, we are always in touch with algorithms. On the one hand, we can improve our thinking ability, and on the other hand, we can improve the code quality.
Good algorithms are not obscure, but amazing.
I hope my answers will help you a little. I wish you success!