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 > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to use Java recursive method to achieve Hanoi Tower". In daily operation, I believe many people have doubts about how to use Java recursive method to achieve Hanoi Tower. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to use Java Recursive method to achieve Hanoi Tower". Next, please follow the editor to study!
Recursion: the programming skill in which a program calls itself.
A procedure that is executed repeatedly (calling itself)
There are conditions to jump out of the repeated execution process (recursive exit)
Import com.lifeibigdata.algorithms.leetcode.TreeNode;public class Recursive {public static void main (String [] args) {/ / System.out.println (factorial (3)); / / tower (2 System.out.println (fib (2)); / / System.out.println (fib_i (1)) System.out.println (factorial_tail); / / System.out.println (is_palindereme (")); / / System.out.println (binary_search (new int [] {1, 2, 3, 4, 5}, 4)); / / System.out.println (binSearch (new int [] {1, 2), 3, 4, 5}, 0, 4, 5) } / * factorial * n! = n * (NMUI 1) * (NMUI 2) *... * 1 (n > 0) * * / static int factorial (int x) {if (0 = = x) {return 1;} else {return x*factorial (x-1) }} / / iterative process static int factorial2 (int n) {return factIterator (1,1, n);} static int factIterator (int result, int counter, int maxCount) {if (counter > maxCount) {return result;} return factIterator ((counter * result), counter + 1, maxCount) } / * * Tower of Hanoi problem * * when nasty 1, move the plate on A directly to C * when n > = 2: * 1, move a plate on A to B (the solution of this step is exactly the same as moving a plate of order n, except that the size of the problem is reduced by 1 order) * 2, move a plate on A to C * 3 Move 1 plate of nMel on B to C. * * / public static void tower (int nonomechar one,char two,char three) {if (nymphomial 1) {move (one,three,1);} else {tower (nlashi 1 Lectrine); / / move the first to the second move (one,three, n) / / move the nth disk from the first to the third pillar tower (nMel 1 recorder twojournal oneMagna three); / / move the second to the third} System.out.println ("- -") / * the first disk of A moves to the second disk of C A, the first disk of B C moves to the third disk of B A, the third disk of B A moves to the first disk of A B, the second disk of A B moves to the first disk of C A, the first disk moves to C * /} / / the output public static void move (char xmemchar y) Int n) {System.out.println (the "+ n +" disk of x + "moves to" + y) } / * full permutation problem * http://blog.csdn.net/xiazdong/article/details/7986015 * / static void swap (int a minint bpenint arr []) {int temp=arr [a]; arr [a] = ARR [b]; arr [b] = temp } public static void perm (int arr [], int begin,int end) {if (end==begin) {/ / output array as soon as it arrives at the recursive exit, this array is fully permuted for (int iMUBI 1) return fib (NMMI 1) + fib (NMMI 2); return 0 } static int fib_i (int a, int b, int n) {if (n = 2) return aqb; else return fib_i (b, aqb, nmer1);} static int factorial_tail (int n acc1,int acc2) {if (n)
< 2) { return acc1; } else { return factorial_tail(n-1,acc2,acc1+acc2); } } /** * fibonacci(n-1,acc2,acc1+acc2)真是神来之笔,原本朴素的递归产生的栈的层次像二叉树一样,以指数级增长,但是现在栈的层次却像是数组,变成线性增长了, 实在是奇妙,总结起来也很简单,原本栈是先扩展开,然后边收拢边计算结果,现在却变成在调用自身的同时通过参数来计算。 小结 尾递归的本质是:将单次计算的结果缓存起来,传递给下次调用,相当于自动累积。 在Java等命令式语言中,尾递归使用非常少见,因为我们可以直接用循环解决。而在函数式语言中,尾递归却是一种神器,要实现循环就靠它了。 */// def Fib(n,b1=1,b2=1,c=3)://// if n b)?(a+1):(b+1); } } /** * *判断一个二叉树是否平衡 */ int isB(TreeNode t){ if(t == null) return 0; int left=isB(t.left); int right=isB(t.right); if( left >= 0 & & right > = 0 & & left-right =-1) return (left heigh-2) {if (a [low] > a [heigh]) max = a [low]; else max = a [heigh];} else {int mid = (low + heigh) / 2; int max1 = Max (a, low, mid); int max2 = Max (a, mid+1, heigh) Max = max1 > max2? Max1: max2;} return max;} / * Digital Tower problem * using Recursive algorithm to solve Digital Tower problem * @ param n number of rows of Digital Tower * @ string of return Digital Tower * / public static String tourData (int n) {String str = new String () If (1 = = n) {str = rowData (n) + "\ n"; return str;} else {str = tourData (nmel1) + rowData (n) + "\ n";} return str;} private static String rowData (int n) {String str = new String (); for (int item0) I target) {int [] narr = new int [arr.length-mid]; System.arraycopy (arr,0,narr,0,arr.length-mid); return binary_search (narr,target);} else if (arr [mid] < target) {int [] narr = new int [arr.length-mid]; System.arraycopy (arr,mid,narr,0,arr.length-mid) Return binary_search (narr,target);} return false;} / * Recursive method to realize binary search. The first position of the * @ param low array * @ param high highest * @ param key the value to look for. * @ return returns a value. * / static int binSearch (int [] Array,int low,int high,int key) {if (low target) {/ / return binary_search (arr,mid,target); / /} else {/ / return binary_search (arr,arrlength-mid,target) / /} / /} / * Rabbit birth problem * / / * stair problem * / / * find all paths in the binary tree and for a certain value * http://z466459262.iteye.com/blog/1115316 * * / / * The difficulty of pure recursion mainly lies in the construction of some conditional expressions and the setting of initial values (the above is the setting of direct expression values) * Recursion is divided into two steps. The general form of recursive sum * * recursive algorithm: void func (mode) {if (endCondition) {constExpression / / basic item} else {accumrateExpreesion / inductive mode=expression / / step expression func (mode) / call itself, recursive} * / so far, the study on "how to use Java recursive method to achieve the Tower of Hanoi" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.