Learning Algorithm -1 + HANOR string inversion (recursive appreciated)

Has not been very understanding of recursive thinking, free today reasonable grounds to think clearly. From the classic Tower of Hanoi to begin.
Here Insert Picture Description
Actually, the problem is very simple, which is a schematic view, a is a starting column, c is the goal posts, b play a role in transit.

During the transfer operation, must ensure that the small cap market under, and one disc can only be moved, eventually all the plates have c column from top to bottom and are in ascending order.

Obviously we can use recursion to solve, where the first affix the correct code. I first with five disk to write, 64 is empathy.

class hanoi{
  public static void main(String[] args)
  {
    goHanoi(5,'a','b','c');
  }

  public static  void goHanoi(int n,char a,char b,char c){
    if(n==1){ 
      System.out.println(n + " from " + a + " to " + c);
      return;
    }      
    goHanoi(n-1, a, c, b);
    System.out.println(n + " from " + a + " to " + c);
    goHanoi(n - 1,b, a, c);
  }
}

Do not look so few lines of code, I think about the journey but also a lot of detours.
Start writing when I first wrote its termination condition.

if(n==1){
      return;
    }     

Then write the following recursive procedure, details of each step in the process of thinking in fact, did not have his execution is how to think clearly, the main way of thinking this is not recursive. In fact, the machine is recursive way of thinking, is not in line with the human way of thinking.

In our usual habit of traversal by us to think about every step of the details, but also need to think about its state after each traverse end. But thinking is recursive, repeating the operation of a regular, every step of the step apart to give, not to be seen as the implementation of a whole , it does not need to be concerned with the state of the end of each step. Like the Tower of Hanoi, when we think we may consider only the bottom layer of n is how the shift, and in front of the n-1 layer as a whole

  • to from a. 1-n-B by c (n-pillars. 1-c by moving from a to b, the following meanings empathy)
  • n from a to c
  • n-1 from b to c by a

To reiterate again, do not use traversal thinking to understand. Do not think about the state after the end of each step, let the machine to perform just fine.
Do not want to control too (funny)

In fact, before there is a problem, when the termination conditions I just wrote a return, resulting in the situation when there is no output to the output tray first came out, but at this writing when I fell into a thought disorder, should be from which where it, a column to column c it? Why I can guarantee it will be the last in a column of it.
The original function where abc is not representative of abc column, so I changed a name to write.

class hanoi{
  public static void main(String[] args)
  {
    goHanoi(5,'a','b','c');
  }

  public static  void goHanoi(int n,char from,char by,char to){
    if(n==1){ 
      System.out.println(n + " from " + from + " to " + to);
      return;
    }      
    goHanoi(n-1, from, to, by);
    System.out.println(n + " from " + from + " to " + to);
    goHanoi(n - 1,by, from, to);
  }
}

This right, and I can understand.
After I was looking for a title.
Reversing a string, such as ABCDE, as output edcba.
In fact, to think of ideas can be recursive. When thinking of the bcde as a whole, x, we just think back to a place of x. When this x smaller that time only one character, recursion ends.

String revertString(String str){
	if(str.size()<=1){
		return str;
	}
	return revertString(str.substring(1)) + str[0];	//substring(1)的意思是截取从字符串的第1个字符到最后一个
}
Published 57 original articles · won praise 3 · Views 6183

Guess you like

Origin blog.csdn.net/qq_39830579/article/details/103989457