递归练习题

来源:互联网 发布:base t以太网端口 编辑:程序博客网 时间:2024/06/05 10:42

Java 语言

1. Write a recursive method that returns the number of 1‟s in the binary representation of N. Use the fact that is equal to the number of 1‟s in the representation of N/2, plus 1, if N is odd.

public static int NumberOfN(int n){    if(n == 1){        return 1;    }else if(n % 2 == 0){        return NumberOfN(n / 2);    }else{        return NumberOfN((n - 1) / 2) + 1;    }}


2. Write the routines wise the following declarations:

public void permute( String str );private void permute( char [ ] str, int low, int high )
The first routine is a driver that calls the second and prints all the
permutations of the characters in String str. If str is “abc”, then the strings that are output are abc, acb, bac, bca, cab,and cba. Use recursion for the second routine.


 调用方法省略

private static void permute(char[ ] list , int low, int high) {        int i;        char t;        if (low == high) {            for (i = 0; i <= high; i++) {                System.out.print(list[i]);            }        }else {            for (i = low; i <= high; i++) {                t = list[low];                list[low] = list[i];                list[i] = t;                perm(list, low + 1, high);                t = list[low];                list[low] = list[i];                list[i] = t;            }        }    }

3. 已知a[n]为整型数组,试写出实现下列运算的递归算法。

1)求数组a中的最大整数。
2)求n个整数的平均值。


private int max(int x[], int n) {        if(n == 1){            return x[0];        }else{            if(x[n - 1] > max(x, n - 1)){                return x[n - 1];            }else{                return max(x, n - 1);            }        }    }


4. Check recursively if the following objects are palindromes:
a. A word
b. a sentence ( ignoring blanks, lower- and uppercase differences, and punctuation marks so that “Madam, I‟m Adam” is accepted as a palindrome)


private boolean palindrome(String str){        str = str.toLowerCase();        for(int i = 0; i < str.length(); i++){            if(str.charAt(i) < 'a' || str.charAt(i) > 'z'){                str = str.replace(str.charAt(i), ' ');            }        }        str = str.replace(" ", "");        System.out.println(str);        if(str.length() != 0 && (str.charAt(0) != str.charAt(str.length() - 1))){            return false;        }else{            if(str.length() == 1 || str.length() == 0){                return true;            }else {                return palindrome(str.substring(1,str.length() - 1));            }        }    }


5. 找出从自然数 1, 2, ..., n 中任取r个数的所有组合, 编一个递归算法.
例子: n = 5 r =3
1 2 3 4 5 5 4 3
542 541 532 531 521 432 431 421 321



<pre name="code" class="java">    private static int a[];    private void call(){        System.out.println("Enter n");        Scanner input = new Scanner(System.in);        int n = input.nextInt();        a = new int[n];        System.out.println("Enter r");        int r = input.nextInt();        a[0] = r;        comb(n, r);    }    
    private void comb(int n, int r){        int i,j;        if(r < 0 || r > n)            return;        for(i = n; i >= r; i--) {            a[r] = i;            if (r > 1) {                comb(i - 1, r - 1);            }else{                for(j = a[0]; j > 0; j--){                    System.out.print(a[j]);                }                System.out.println();            }        }    }


6. HANOI TOWER

    private static void hanoi(){        System.out.println( "Enter number of disks" );        Scanner input = new Scanner(System.in);        int n = input.nextInt();        System.out.println("The move are:");        moveDISKs(n, 'A', 'B', 'C');    }    private static void moveDISKs(int n, char fromTower, char toTower, char auxTower) {        if (n == 1) {            System.out.println("Move disk " + n + " from " + fromTower + " to " + toTower);        }else {            moveDISKs(n - 1, fromTower, auxTower, toTower);            System.out.println("Move disk " + n + " from " + fromTower + " to " + toTower);            moveDISKs(n - 1, auxTower, toTower, fromTower); }    }


0 0
原创粉丝点击