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)
*/
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)
*/
相关文章推荐
- java面试题【递归】和【循环】的考察7月中旬
- java递归常见的面试题
- java递归面试题
- 常见Java面试题 :迭代(iteration)和递归(recursion)
- 面试题—Java递归实现约瑟夫环
- 常见Java面试题 :迭代(iteration)和递归(recursion)
- 由字符串反转(使用递归)引申出来一道Java面试题
- 递归做法(JAVA面试题续)
- 常见的几道关于递归的JAVA面试题
- Java面试题中的关于单例模式、冒泡排序、递归
- 面试题-程序题-Java递归实现遍历文件目录(所有文件夹和子文件)
- 剑指Offer 面试题25:合并两个排序的链表(递归+非递归) Java代码实现
- JAVA面试题---用java程序遍历二叉树(递归与非递归)
- 由字符串反转(使用递归)引申出来一道Java面试题
- 【转】常见Java面试题 – 第四部分:迭代(iteration)和递归(recursion)
- 常见Java面试题 :迭代(iteration)和递归(recursion)
- 经典面试题:单链条反转的递归与非递归方法及分析[Java]
- 由字符串反转(使用递归)引申出来一道Java面试题
- 面试题二: java 实现二叉树的中序优先遍历,不能用递归
- 百度面试题的java实现