java遞迴常見的面試題

來源:互聯網
上載者:User
package math;public class RevertANumber {    /*     * 一列數的規則如下: 1、1、2、3、5、8、13、21、34...... 求第30位元是多少, 用遞迴演算法實現。     * */    static int find30(int n){        if (n <= 0)            return 0;            else if(n > 0 && n <= 2)            return 1;        return find30(n-1)+find30(n-2);               }          /*     * 將一整數逆序後放入一數組中(要求遞迴實現) Ex : 1234 變為 {4,3,2,1}     */    static int revert(int rs[], int i, int number) {        if (i < rs.length) {            rs[i] = number % 10;            number = (number - number % 10) / 10;            return revert(rs, i + 1, number);        } else {            return 0;        }    }    /*     * 遞迴實現迴文判斷(如:abcdedbca就是迴文,判斷一個面試者對遞迴理解的簡單程式)     */    static boolean loopWord(String str, int i) {        if (str.charAt(i) == str.charAt(str.length() - 1 - i)) {            if (i == (str.length() + 1) / 2)                return true;            return loopWord(str, i + 1);        } else {            return false;        }    }    /*     * 分解成質因數(如435234=251*17*17*3*2,據說是華為筆試題)     */    static int dividePrimeToFactors(int num, int factor) {        while ((factor < num) && (num % factor != 0))            factor++;        System.out.print(factor + " ");        num = num / factor;        if (factor >= num)            return factor;// 結束判斷        return dividePrimeToFactors(num, factor + 1);    }    public static void main(String[] args) {              System.out.println(find30(30));               int number = 1234;        int[] rs = new int[(number + "").length()];        revert(rs, 0, number);        for (int i = 0; i < rs.length; i++) {            System.out.print(rs[i]);        }        System.out.println(loopWord("1234321", 0));        dividePrimeToFactors(435234, 2);    }}================================================================================//遞迴 尋找迷宮出路package math;public class Maze {    int MAX_SIZE = 8;    int h[] = { 0, 1, 0, -1 };    int v[] = { -1, 0, 1, 0 };    // 這樣組成了向周圍4個方向 依次是 ← ↓ → ↑    char Maze[][] = {            { 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X' },            { ' ', ' ', ' ', ' ', ' ', 'X', 'X', 'X' },            { 'X', ' ', 'X', 'X', ' ', ' ', ' ', 'X' },            { 'X', ' ', 'X', 'X', ' ', 'X', 'X', ' ' },            { 'X', ' ', 'X', 'X', 'X', 'X', 'X', 'X' },            { 'X', ' ', 'X', 'X', ' ', ' ', ' ', 'X' },            { 'X', ' ', ' ', ' ', ' ', 'X', ' ', ' ' },            { 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X' } };    void FindPath(int X, int Y) {        if (X == MAX_SIZE || Y == MAX_SIZE) {//表明指標已經移動到邊緣 準備輸出            for (int i = 0; i < MAX_SIZE; i++) {                for (int j = 0; j < MAX_SIZE; j++) {                    if (j < MAX_SIZE - 1) {                        System.out.print(Maze[i][j] + " ");                    } else {//到達列末尾 準備換行                        System.out.println(Maze[i][j]);                    }                }            }        } else {                checkAround(X, Y);        }    }       void checkAround(int X, int  Y){        //把尋找周圍4個路徑放在前面 這樣可能不好理解//        for (int k = 0; k < 4; k++) {//上下左右四個方向掃描//            if (X >= 0 && Y >= 0 && Y < MAX_SIZE && X < MAX_SIZE//                    && ' ' == Maze[X][Y]) {//                Maze[X][Y] = '.';//                FindPath(X + v[k], Y + h[k]);// 檢查周圍4個方向是不是也是o//                Maze[X][Y] = ' ';//            }//        }               if (X >= 0 && Y >= 0 && Y < MAX_SIZE && X < MAX_SIZE                && ' ' == Maze[X][Y]) {            Maze[X][Y] = '.';            for (int k = 0; k < 4; k++) {                FindPath(X + v[k], Y + h[k]);// 檢查周圍4個方向是不是也是o               }            Maze[X][Y] = ' ';//沿著路線走 最終有的路可能是走不通 標記回來        }    }    public static void main(String[] args) {        Maze maze = new Maze();        maze.FindPath(1, 0);// 從 Maze[1][0]進入    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.