C # implement tower of Hanoi,

Source: Internet
Author: User

C # implement tower of Hanoi,

The origins of the Tower:
Pandata is a toy derived from Indian mythology.
When God created the world, he made three diamond pillars and placed 64 gold disks in order from bottom to bottom.
God ordered the Brahman to re-place the disc from below in order of size on another pillar. It is also stipulated that the disc cannot be enlarged on a small disc, and only one disc can be moved between the three pillars at a time.
It was predicted that the universe would flash into destruction at the completion of the event. Some people believe that Brahman is still moving the disc for a moment.
Life Cycle tower and Universe:
If it takes one second to move a disc, when will the universe be destroyed when all the 64 discs fall together?
Let's consider how many times the 64 disks need to be moved. One time is of course one time, two times is three times, and three times is used seven times.
When one disc is created, the power of 2 is reduced by 1.
When two disks exist, the power of 2 is reduced by 1.
When three disks exist, the power of 2 is reduced by 1 to the power of 3.
When there are four disks, the power of 2 is reduced by 1 to the power of 4.
When there are five disks, the power of 2 is reduced by 1 to the power of 5.
........
When n disks exist, the Npower of 2 is reduced by 1.
That is to say, when n is 64, it is (the power of 2 is reduced by 1.
Therefore, it takes one second to move a disc,
The lifetime of the universe = the power 64 of 2 minus 1 (seconds)
It takes about 580 billion years for a year = 60 seconds x 60 Minutes x 24 hours X days.
It is said that the age of the universe is about 15 billion years.
The quanta issue has a high research value in the field of mathematics,
And it is still being studied by some mathematicians,
It is also a puzzle game that we like to play,
It can help develop intelligence and stimulate our thinking.
C language implementation of Linglong
# Include "stdio. h"
Void move (char x, char y)
{
Printf ("% c --> % c/n", x, y );
}
Void hanoi (int n, char one, char two, char three)
{
If (n = 1) move (one, three );
Else {
Hanoi (n-1, one, three, two );
Move (one, three );
Hanoi (n-1, two, one, three );
}
}

 


Symbol in C Language <Yes

Left shift operator (<)

Removes all the binary bits of an operation object from the left and adds 0 to the right ).

For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,

Move 1 to the left and then a = a * 2;

If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)

Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.

The operand shifts one digit to the right, which is equivalent to dividing the number by 2.

For example, a = a> 2 shifts the binary bit of a two places to the right,

0 or 1 to see whether the number is positive or negative.

Symbol in C Language <Yes

Left shift operator (<)

Removes all the binary bits of an operation object from the left and adds 0 to the right ).

For example, a = a <2 shifts the binary bits of a two places to the left and complements 0 to the right,

Move 1 to the left and then a = a * 2;

If the left shift does not include 1 in the Discard high position, then shifts one bit left, which is equivalent to multiplying the number by 2.
Shift right operator (>)

Shifts all the binary bits of a number to several places to the right, and adds 0 to the left of the positive number, 1 to the left of the negative number, and discards the right of the negative number.

The operand shifts one digit to the right, which is equivalent to dividing the number by 2.

For example, a = a> 2 shifts the binary bit of a two places to the right,

0 or 1 to see whether the number is positive or negative.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.