java 遞迴 面試題
阿新 • • 發佈:2019-01-26
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))
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)
*/