012 keep it up, 012 reading Tower
Tower of Hanoi: There are three adjacent columns, labeled as A, B, C, and A, stacked with n disks of different sizes in pyramid shape from bottom to top, how should I move all the plates one by one to Column B, and each time I move the same column, there cannot be big plates above the small plate?
The tower of Hanoi is a very classic problem. It should be mentioned in recursion. If we do not have the prior knowledge of recursion to answer this question directly, we often feel that we do not know how to start. Recursion can solve this problem very beautifully.
The key to recursion is to define a function first, so you don't have to rush to implement it, but you need to clarify its functions.
For the tower of Hanoi Problem, we define the following function prototype:
Void hanoi (int n, char src, char bri, char dst );
We should not care about how it is implemented, but clarify that its functions are:
Move n discs from the column src to the column dst, with the help of the column bri (bridge ).
Note: The numbers of n disks from top to bottom are 1 to n, indicating that the disks are small to large.
During the process of moving, the large disc is not allowed to be placed on the small disc.
Okay. Now that recursion is required, we still use this function in this function. That is to say, the hanoi operation will be used to complete the steps in this task, but the parameters may be different. We define a group of tuples to indicate the state of the three pillars: (the disc on src, the disc on bri, And the disc on dst) the initial state is :( 1 ~ N, 0, 0) indicates that there are 1 to n disks on src, and there are no disks on the other two columns. The target status is: (0, 0, 1 ~ N) indicates that there are 1 to n disks on dst, and there are no disks on the other two columns. Since the Largest Disc n is placed at the bottom of dst and the large disc cannot be placed on the small disc, such an intermediate state must exist: (n, 1 ~ N-1, 0) to move the Largest Disc n to the bottom of dst. At this time, someone will ask, how do you think of this intermediate state instead of others? Good question. Because the tools (available functions) at hand are only hanoi, I naturally want to create a scenario where this function can be used, rather than other scenarios.
The initial status is: (1 ~ N, 0, 0)
The intermediate status is: (n, 1 ~ N-1, 0)
From the initial state to the intermediate state, the Operation hanoi (n-1, src, dst, bri) will be available. That is, the n-1 disc is moved from src to bri, and the column dst can be used in the middle.
The next step is to move the disc n from src to dst, which can be output directly:
Cout <"Move disk" <n <"from" <src <"to" <dst <endl;
The status after this operation is:
(0, 1 ~ N-1, n)
Then, use the hanoi function to move n-1 disks from bri to dst. The column src, hanoi (n-1, bri, src, dst) can be used in the middle, and the final state is obtained after the operation:
(0, 0, 1 ~ N)
These operations are combined with three lines of code:
Hanoi (n-1, src, dst, bri );
Cout <"Move disk" <n <"from" <src <"to" <dst <endl;
Hanoi (n-1, bri, src, dst );
Finally, we need to recursively stop conditions. When does recursion end? When n is equal to 1, when there is only one disc, you can directly move it from src to dst:
If (n = 1)
{
Cout <"Move disk" <n <"from" <src <"to" <dst <endl;
}
void hanoi(int vN, char vSrc, char vBriage, char vDest){if (vN == 1){std::cout << "move the dist "<< vN << " from " << vSrc << " to " << vDest << std::endl;return;}hanoi(vN-1, vSrc, vDest, vBriage);std::cout << "move the dist "<< vN << " from " << vSrc << " to " << vDest << std::endl;hanoi(vN-1, vBriage, vSrc, vDest);}
Useful sentence patterns for English College Entrance Examination Compositions
I. Argumentative papers
Argumentative papers
1. discussion topic
Recently, we have a heated discussion about whether we should build this factory Recently, we have had a heated discussion about whether we shoshould build a factory here.
2. Different Opinions have different opinions on this issue.
There are two different opinions. Opinions are divided on the question.
Opinions are mainly divided into two groups.
3. Support opinion Many people support this opinion, namely, people favor this opinion.
Quite a few people support this view.
4. Objection many people disagree with this opinion A large number of people are against it.
When a student disagrees with that point of view.
5. There are many reasons for this situation
Many people are fat mainly because they eat too much. There are too reasons for this.
The main reason why available people are fat is that they eat too much.
6. weigh the advantages and disadvantages of moving to the Beijing Zoo
We must weigh The pros and cons of moving Beijing zoo the disadvantages of moving the zoo outweigh The advantages.
We have to balance the disadvantages of moving the zoo against the advantages.
7. Some people think that smoking is a kind of enjoyment. Some people think that smoking is an enjoyment.
Some people hold the view that smoking is an enjoyment.
8. We have the responsibility to protect the environment. We are supposed to shoulder the responsibility for protecting the environment.
9. Take measures. We should take Immediate measures to Immediate actions shoshould be taken.
10. We will benefit a lot if we move to the zoo. We can benefit a lot from moving the zoo.
Ii. Narrative
Narrative
1. It makes sense This experience is very meaningful. This is/was a really meaningful experience.
2. Have fun. We have fun. We enjoyed a great deal the fun and laughter.
3. Unforgettable Experience. I find it a really memorable experience.
4. a lot of trips to the United States are very rewarding. Our trip to America was a very rewarding experience.
5. Thank you very much for your kindness.
Frequently used phrases in high school English
Frequently used phrases in high school English
A
Glossary
Above all
According
Act upon each other interaction
Adapt oneself to adapt yourself ......
Adapt to adapt
Add...... Join
Add up to total
Address oneself to direction... Speech; and... Communication
A few (yes): a few
A great quantity of (only a handful or an unmeasurable number)
A good deal of (n) Many, a large number
A large amount of (n) a large number
A little (yes) some, a little bit
A lot
A number
Above all, first of all, especially
After all, after all
Ahead... Before
All but almost, almost;... Both
All of a sudden suddenly
All over is everywhere, and all ends.
All right is satisfactory; (healthy) Good; Good, OK, yes
In all, total
All the same is still;
All the time, always
And so on
Apart from Division... Except... Besides (still)
As... as, like,... Same
As far away; just... As
As follows:
As for, about
Ask for leave
As long as, if; since, because
As soon... Just... Convenient
As though seems, as if
As for, about
As usual is used as usual-
As well, and
As well as Division... Besides (also), both... Again
Ask for request, required
At a loss is puzzled and at a loss
At all (used for negative sentences)
At all costs at any cost, no matter what
At all events
At any rate, at least
At least comfort)
At first, first
At hand is close at hand.
At last
At least
At most, Max.
At no time never, never.
At once, immediately; at the same time, together
At present currently
At the cost... Cost
At the moment
At the same time as the same time; however
At the sight of seeing... Just
At the thought of the thought... Just
B
Glossary
Back and forth (front and back) back and forth, repeatedly
Back up support; backward
Be absorbed in concentrate on the spirit
Be described
Be in favor of support
Be made up... Composed... Composition
Be short of is missing, insufficient; not reached
Bear... in mind remember (something)
Because of because
Before long later
Be worth doin... the remaining full text>