1. The concept of divide and conquer
Divide and conquer is an algorithm that uses recursion to solve problems, the main trick is to divide a large complex problem into multiple child problems, which can be used as termination conditions or solved in a recursive step, and the resolution of all the child problems together constitutes the solution to the original problem.
2. Advantages and disadvantages of divide and conquer
A divide-and-conquer algorithm usually includes the invocation of one or more recursive methods, when these calls separate the data into separate collections to handle smaller collections, the rule-and-conquer strategy is highly efficient, and when the data is decomposed, the divide-and-conquer strategy can result in a lot of duplication, resulting in reduced performance.
3. Drawing the ruler procedure analysis explanation
Drawing a ruler is a simple application of a divide-and-conquer strategy, and the ruler is a sequence of 1-inch units of length, the end of each cell has the longest mark, the mark at 1/2 inches of each inch unit is shorter than the end, the mark at 1/4 is shorter than 1/2, 1/8 is shorter than 1/4, and a program is written, On a line, draw the marker at regular intervals, with a specific size mark at a particular location.
Analysis: In a straight line, we can first divide the line into two, and then split the two separated. Until a certain precision is met, for example, with a minimum scale of 1/8 inches, drawruler as the function of the ruler, using the ends of a segment in the Drawruler function (the starting point (startpos), the endpoint (Endpos)), and the variable h as the argument, The base height of the tag is baseheight and the height of the marker should be h*baseheight, the drawing of the ruler can be analyzed as follows:
Midpoint of the computed interval (0.0,1.0): Midpos = (startpost+endpos)/2; draw a marker at 1/2 in the midpoint, a height of 3*baseheight
Separate the midpoint into two straight lines, then use the Drawrule function, the corresponding starting point, the end point (0.0,0.5) and (0.5,1.0), parameter h-1, which can make the height shorter
Step 2 (h=2)
Midpos = (0.0+0.5)/2 (1/4), height is 2*baseheight
Midpos = (0.5+1.0)/2 (3/4) height is 2*baseheight
Step (h=1)
Marked at 1/8 and 7/8 respectively, the method of calculation
Midpos = (0.0+0.25)/2 (1/8) height is baseheight
Midpos = (0.75+1)/2 (7/8) height is baseheight
A diagram can be used to indicate the following
We can treat the tokens produced in succession as the nodes of a binary tree. Root h is the initial value. is a 1/2 mark, each of which has two tokens. As shown in the following figure
4. Executable procedure Documents
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Text;
Using System.Windows.Forms;
namespace Drawruler {public partial class Form1:form {public Form1 () {InitializeComponent (); private void Form1_Load (object sender, EventArgs e) {} void Drawruler (float startpos, float endpos, int h) {FL
Oat baseheight = 4;
if (H > 0) {float Midpos = (startpos + endpos)/2;
float height = h * baseheight;
Drawmark (midpos, height);
Drawruler (Startpos, Midpos, h-1);
Drawruler (Midpos, Endpos, h-1); } void Drawmark (float pos, float height) {using (Graphics g = this.)
CreateGraphics ()) {Float Xoffset = + pos;
float yoffset = 100-height;
SolidBrush brusuh = new SolidBrush (color.black);
Pen p = new Pen (brusuh, 1);
G.drawline (P, Xoffset, Yoffset, Xoffset, 100); } private void Form1_paint (object sender, PaintEventArgs e) {#region First draw a straight using (Graphics g = e.graphics)
{Float Xoffset = 100;
float yoffset = 100;
int len = 300;
SolidBrush brusuh = new SolidBrush (color.black);
Pen p = new Pen (brusuh, 2);
G.drawline (P, Xoffset, Yoffset, Xoffset + len, yoffset);
#endregion Drawruler (0, 300, 3);
}
}
}
5. Code download
Http://xiazai.jb51.net/201606/yuanma/DrawRuler (jb51.net). rar
The above is the entire content of this article, I hope to give you a reference, but also hope that we support the cloud habitat community.