Extreme recursive (thinking reversal)

xiaoxiao2021-03-06  75

REL = file-list href = "./ Regender is the basis of program design, and it is one of the potential to dig. Files / fileList.xml>>

Recursive is the basis of programming, and it is one of the potential to capture.

Recursive is the foundation of programming, and it is one of the most potential design patterns. There are two modes of recursive, one is based on the internal recursive of the program stack, the most convenient use of the method, and is the most convenient to the recursive level, but the exterior stack is often required. of. This article will introduce two most basic recursive issues, which is suitable for beginners, but the method of solving this problem is elegant enough, so it is necessary to have a certain reference value for colleagues who can skilled C . Hanno Tag issues to recurrent, you can't talk about Hannota. This is a very famous and very interesting recursive issue. It is required to use three bases to move a bunch of discs, requiring the disc from the A base. Move to the C base, the order of movement must be that each movement ensures that the disc is larger than the lower part of the upper part of the disc. Everyone can design a official solution from Tan's C program, but it is definitely not the most simple and elegant. Below I gave my solution, calculated as a throw, I hope to lead the jade in your hands.

#include

#include

Unsigned long time = 0;

Void Hanor (Int I, Char A, Char B, Char C)

{

IF (i> = 2)

Hanor (I-1, A, C, B);

COUT << I << ":" << a << "--->" << c << "<< Time << Endl;

IF (i> = 2)

Hanor (I-1, B, A, C);

}

int main ()

{

Hanor (16, 'a', 'b', 'c');

System ("pause");

Return 0;

}

Eight Queens

The eight queen is another recursive classic problem. The problem requires that the Queen is charming in an international picture board, and the Queen is not allowed to exist in rice-type plaid. This problem is more than The problem is more complicated, and the solution is also more interesting. In order to get a more elegant and concise solution, I will take the liberty to throw a brick.

#include

Using namespace std;

#include

Int Queens [9];

Bool IS;

INT TOTAL = 0;

Void Run (Int Num)

{

For (INT i = 1; i <= 8; i )

{

Queens [NUM] = i;

IS = true;

For (int J = 1; j

IF (Queens [J] == Queens [NUM] || Queens [J] (Num-j) == Queens [NUM] ||

Queens [J] - (NUM-J) == Queens [NUM])

{

IS = false;

Break;

}

IF (IS)

{

IF (NUM == 8)

{

For (int K = 1; k <= 8; k )

COUT << Queens [K] << ""; cout << Total << Endl;

}

IF (Num <8)

Run (NUM 1);

}

}

}

int main ()

{

Run (1);

System ("pause");

Return 0;

}

I deliberately not comment, I hope such a short code can be willing to read the analysis.

Designing recursive procedures is very interesting and challenging, how is elegant and simple, small in memory, high efficiency, is needed, and it is a core technology that builds many practical applications, a large amount of data structure and algorithms, such as btree, bucket In the form of expression, the regular expression is used, and the recursive is used. I hope that this article can learn from everyone.

转载请注明原文地址:https://www.9cbs.com/read-97919.html

New Post(0)