java--Hanoi

Source: Internet
Author: User

You can remember the Spring Festival evening show. How many steps does it take to put an elephant in the refrigerator?

Today, I'm going to write about Hanoi, with three magical pillars: honesty, bravery, integrity. One of the pillars from the big to the small, from the ground up in turn emitted four magic rings, you need to the four magic rings on the large top of the small, can not be magnified on the small;

import Java.util.Scanner; Public classSSR {Static intStep =0;  Public Static voidMain (string[] args) {//HanoiHanio (4,"Honest","Brave","Integrity"); }                                     Public Static voidHanio (intnum, string A, string B, string c) {                  if(num = =1) {move (NUM,A,C); } Else{Hanio (num-1, A, C, b);                      Move (NUM,A,C); Hanio (Num-1, B, A, c); }              }               Public Static voidMoveintnum, String a,string b) {Step++ ; System. out. println ("Section"+step+"Step, plate"+num+"from"-am"the tower moves to"+b+"Tower"); }    }

Operation Result:

1th, plate 1 moves from the Honest Tower to the 2nd step of the Brave tower, plate 2 from the Honest Tower to the 3rd step of the Upright tower, plate 1 from the brave tower to the upright tower 4th step, plate 3 from the honest Tower to the Brave Tower 5th step, plate 1 from the upright tower to the Honest Tower 6th step, plate 2 from the upright tower to the Plate 1 moved from the Honest Tower to the 8th step of the Brave tower, plate 4 moved from the Honest Tower to the 9th step of the Upright tower, plate 1 moved from the brave tower to the upright tower 10th step, plate 2 from the brave tower to the faithful tower 11th step, plate 1 from the upright tower to the Honest Tower 12th step, plate 3 from the brave tower to the Plate 1 move from the Honest Tower to the 14th step of the Brave tower, plate 2 moves from the Honest tower to the 15th step of the upright tower, plate 1 moves from the brave tower to the upright tower

Isn't it interesting?

java--Hanoi

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.