您的位置:首页 > 职场人生

java 递归 面试题

2014-05-13 14:39 176 查看
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 void dividePrimeToFactors(int num, int factor) {

while ((factor < num) && (num % factor != 0))

factor++;

if(factor<=num)

{

System.out.print(factor + " ");

num = num / factor;

dividePrimeToFactors(num, 2);

return;

}

else if (factor >num)

{

System.out.println(num);

return ;// 结束判断

}

}

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 agrisom;

import java.util.EmptyStackException;

import java.util.Stack;

/**

* 递归_迷宫

*/

/**

* 位置类

*/

class Position{

public int row;

public int col;

public Position(int row,int col){

this.row=row;

this.col=col;

}

}

public class Maze {

public Stack<Position> stack=new Stack<Position>(); //保存迷宫路径

public int array[][] ={

{1,1,1,1,1,1,1,1},

{1,0,0,0,1,0,0,1},

{1,0,1,1,1,0,0,1},

{0,0,1,0,1,0,0,0},

{1,1,1,0,1,0,0,1},

{1,1,0,1,1,1,1,1},

{0,0,0,1,0,0,0,0},

{0,0,1,1,1,1,1,1},

};

//定义的方格数组需在迷宫内

private boolean value(int hang,int lie){

if(hang >= 0 && hang < array.length &&

lie >=0 && lie< array.length &&

array[hang][lie] == 1){

return true;

}else{

return false;

}

}

private boolean discovery(int row, int col){

boolean pass =false;

if(value(row,col)){

//将走过的位置标记为2

array[row][col] = 2;

Position pos=new Position(row,col);

this.stack.push(pos);

//判断是否到达终点.

if(row == array.length -1 && col == array.length - 1){

pass = true;

}else{//往右走

pass = discovery(row,col + 1);

//往下走

if(pass==false)

pass = discovery(row + 1, col);

//往左走

if(pass==false)

pass = discovery(row , col-1);

//往上走

if(pass==false)

pass = discovery(row - 1, col);

}

if(pass==true) {

array[row][col] = 3;

}else{

this.stack.pop();

}

}

return pass;

}

public static void main(String[] args) {

Maze mi = new Maze();

int[][] c = mi.array;

//输出走之前的迷宫

for(int i = 0;i<c.length;i++){

for(int j = 0; j<c.length;j++){

// System.out.print(mi.array[i][j]+" ");

if(mi.array[i][j] == 1)

System.out.print("□"+" ");

if(mi.array[i][j] == 0)

System.out.print("■"+" ");

}

System.out.println();

}

System.out.println(mi.discovery(0,0));

//输出走过之后的迷宫

for(int i = 0 ;i<= c.length-1;i++){

for(int j = 0; j<= c.length-1;j++){



// System.out.print(c[i][j]+" ");

if(c[i][j] == 1)

System.out.print("□"+" ");

if(c[i][j] == 0)

System.out.print("■"+" ");

if(c[i][j] == 3)

System.out.print("○"+" ");

if(c[i][j] == 2)

System.out.print("●"+" ");

}

System.out.println();

}

System.out.println();

//输出路径

Stack<Position> stack=new Stack<Position>();

Position pos=mi.stack.pop();

while(pos!=null){

stack.push(pos);

try{

pos=mi.stack.pop();

}catch(EmptyStackException e){

break;

}



}

pos=stack.pop();

int length=stack.size();

while(pos!=null){

if(1==length){

System.out.print("("+pos.row+","+pos.col+")");

}else{

System.out.print("("+pos.row+","+pos.col+")→");

length=stack.size();

}

try{

pos=stack.pop();

}catch(EmptyStackException e){

break;

}

}

}

}

/**

运行结果:

□ □ □ □ □ □ □ □

□ ■ ■ ■ □ ■ ■ □

□ ■ □ □ □ ■ ■ □

■ ■ □ ■ □ ■ ■ ■

□ □ □ ■ □ ■ ■ □

□ □ ■ □ □ □ □ □

■ ■ ■ □ ■ ■ ■ ■

■ ■ □ □ □ □ □ □

true

○ ○ ○ ○ ○ ● ● ●

□ ■ ■ ■ ○ ■ ■ ●

□ ■ □ □ ○ ■ ■ ●

■ ■ □ ■ ○ ■ ■ ■

□ □ □ ■ ○ ■ ■ ●

□ □ ■ ○ ○ ● ● ●

■ ■ ■ ○ ■ ■ ■ ■

■ ■ □ ○ ○ ○ ○ ○

(0,0)→(0,1)→(0,2)→(0,3)→(0,4)→(1,4)→(2,4)→(3,4)→(4,4)→(5,4)→(5,3)→(6,3)→(7,3)→(7,4)→(7,5)→(7,6)→(7,7)

*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: