以下是在最近面试过程中碰到有关递归问题的题目: /** * 汉诺塔问题 * * @param num * 盘子数 * @param a * 柱子a * @param b * 柱子b * @param c * 柱子c */ public static void hannota(int num, char a, char b, char c) { if (num == 1) { move(1, a, c); return; } else { hannota(num - 1, a, c, b); move(num - 1, a, c); hannota(num - 1, b, a, c); } } static void move(int num, char source, char target) { System.out.println("moving " + num + " from " + source + " to " + target); } public class Recursion { public static void main(String[] args) { System.out.println("the last pepole's age is :" + getAge(10)); System.out.println("the binary of the algorism is : " + getBinary(100)); hannota(3, 'a', 'b', 'c'); } /** * 第一个人的年龄是10,以后每个人的年龄都比前一个人的年龄大2岁,求第n个人的年龄 * * @param people * @return */ public static int getAge(int people) { if (people == 1) { return 10; } else { return 2 + getAge(people - 1); } } /** * 求一个十进制数字的二进制形式是多少 * * @param num * @return */ public static String getBinary(int num) { if (num < 2) { return num + ""; } else { return getBinary(num / 2) + num % 2 + ""; } } /** * 写一个方法,要求参数int类型,如:传入一个12345,返回结果54321。用递归 */ public class Reverse { public static void reverse(int number) { if (number < 10) { System.out.print(number); } else { int num = number % 10; System.out.print(num); reverse(number / 10); } } public static void main(String[] args) { reverse(123); } }