Java Study Questions

Source: Internet
Author: User

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

Related Article

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.