In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use Java recursion to realize the game of Tower of Hanoi. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.
It is easy to imagine that the game of Tower of Hanoi can be realized by recursion. Because we want to move n (n > 1) plates from the "source" column to the "target" column, we need to first move a plate from the "source" column to the "auxiliary" column, then move the bottom plate to the target post, and finally move the nmur1 plate on the "auxiliary column" to the target post. When moving directly to the target column, it is also the exit of recursion.
With the groundwork for the above ideas, you can begin to implement the code.
Public class HanoiDemo {public static int hanoiCalledCount = 0 int / member variable record number of operations / Hanotta game is a two-way recursive public static void main (String [] args) {hanoi (3, "A", "B", "C"); System.out.println ("execute" + hanoiCalledCount+ "step");} public static void hanoi (int njingString source,String target,String assist) {if (n)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.