1. Look at the code, what special do you find?
The return value type is not the same as the parameter type.
2. Solving the questions of the Nottingham Tower
Code:
public class towersofhanoi{
Used to move a plate recursively
public static void Solvetowers (int disks, int sourcepeg,
int destinationpeg, int temppeg)//The first parameter is the total number of plates, the second parameter means the starting disk, the third parameter is the target disk, the fourth parameter is the auxiliary disk
{
Only one plate, moving directly from the starter tray to the target disk
if (disks = = 1)
{
System.out.print (sourcepeg+ "--" +destinationpeg+ "");
Return
}
Move the N-1 plate from the starter tray to the auxiliary tray
Solvetowers (Disks-1, Sourcepeg, Temppeg, destinationpeg);
The last plate on the starter plate is moved to the target disk.
System.out.print (sourcepeg+ "--" +destinationpeg+ "");
On the auxiliary plate, move the N-1 plate to the target plate.
Solvetowers (Disks-1, Temppeg, Destinationpeg, sourcepeg);
}//End
public static void Main (string[] args) {
int startpeg = 1; Start disk
int endpeg = 3; Target Disk
int temppeg = 2; Auxiliary disk
int totaldisks = 4; Total Plates
Solvetowers (Totaldisks, Startpeg, Endpeg, temppeg);
}
}
Program Design ideas: Define a function for recursive operation, the total plate is n,1 for the starting disk, 2 for the auxiliary disk, 3 for the target disk, the first 1 on the N-1 plate moved to 2, then the last plate 1 on the 3, and finally the 2 on the N-1 plate moved to 3, and then recursion, until completed.
3. Determine whether a string is a palindrome by recursion
A palindrome is the same as reading and reading the opposite, such as "I am who I am", First judge 1th and Nth, and then judge the 2nd and the first N-1 ... By recursion, when there is only one or 0 characters at the end, the program terminates and ends.
Java Study Questions