因为对递归的不了解,学习了如下的实例:
1、三角数字
可以使用循环方式,也可以使用递归方法。
package triangle;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.*;
public class Triangle {
static int number;
public static void main(String[] args) throws IOException {
System.out.println("enter a number:");
/*Scanner scan = new Scanner(System.in);
while(scan.hasNext())
{
number =scan.nextInt();
}*/ ??
number=getInt();
int answer =triangle(number);
System.out.println("triangle:"+answer);
}
public static int triangle(int n)
{
if(n==1)
return 1;
else
return (n+triangle(n-1));
}
public static String getString() throws IOException
{
//InputStreamReader是字节流通向字符流的桥梁
//OutputStreamWriter是字符流通向字节流的桥梁
InputStreamReader is = new InputStreamReader(System.in);
//为提高效率,将字符串进行缓冲区技术高效操作,使用bufferedreader
BufferedReader b = new BufferedReader(is);
//readline是字符流bufferedreader类中的方法
String s =b.readLine();
return s;
}
public static int getInt()throws IOException
{
String s = getString();
return Integer.parseInt(s);
}
}
过程如下图:
其实递归的本质是数学归纳法:是一种通过自身的语汇定义某事物自己的方法。
tri(n)=1 if n=1;
tri(n)=n+tri(n-1) if n>1;
2、阶乘
与三角数字相似,只不过基值条件为当n=0时,而不是1时。
int factorial(int n)
{
if(n==0)
return 1;
else
return (n*factorial(n-1));
}
3、变位字
假设要列出一个指定单词的所有变位字,也就是列出该单词的全排列,不管是否是单词,则称为这个工作是变位一个单词或称为全排列一个单词。
当你自己写出时会发现可以写出的排列的数量是单词字母数的阶乘!
package triangle;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Anagram {
static int size;
static int count;
static char[] arrchar=new char[100];
public static void main(String[] args) throws IOException {
System.out.println("enter a word:");
String input= getString();
size= input.length();
count=0;
for(int j=0;j<size;j++)
{
arrchar[j]=input.charAt(j);
}
doanagram(size);
}
public static void doanagram(int newsize)
{
if(newsize==1)
return ;
for(int j=0;j<newsize;j++)
{
doanagram(newsize-1);
if(newsize==2)
display();
rotate(newsize);
}
}
public static void rotate(int newsize)
{
int j;
int position=size-newsize;
char temp=arrchar[position]; //save first letter
for(j=position+1;j<size;j++) //shift others left
arrchar[j-1]=arrchar[j];
arrchar[j-1]=temp; //put first on right ?
}
public static void display()
{
if(count<9)//?
System.out.println(" ");
System.out.print(++count +" ");
for(int j=0;j<size;j++)
System.out.print(arrchar[j]);
System.out.print(" ");
System.out.flush();
if(count%6==0)
System.out.print("");//??
}
public static String getString() throws IOException
{
InputStreamReader is= new InputStreamReader(System.in);
BufferedReader b = new BufferedReader(is);
String s =b.readLine();
return s;
}
}
结果:
enter a word:
cat
1 cat
2 cta
3 atc
4 act
5 tca
6 tac
4、递归的二分查找
想用最少的比较次数在一个有序数组中找出一个数据项,用二分法
package triangle;
public class BinarySearch {
public static void main(String[] args) {
int max=50;
ordarray arr =new ordarray(max);
arr.ins(34);
arr.ins(22);
arr.ins(11);
arr.ins(66);
arr.insertion();
arr.display();
int searchkey=66;
if(arr.find(66)<=arr.size())
System.out.println("found" +searchkey);
else
System.out.println("can't found");
}
}
class ordarray
{
private long[] a;
private int num;
public ordarray(int max)
{
a=new long[max];
num=0;
}
public int size()
{
return num;
}
public int find(long searchkey)
{
return recfind(searchkey,0,num-1);
}
private int recfind(long searchkey,int low,int high)
{
int cur=(low+high)/2;
if(a[cur]==searchkey)
{System.out.println("找到啦");
//System.out.println(cur);
return cur;
}
else if(low>high)
return -1;
else
if(a[cur]>searchkey)
return recfind(searchkey,low,cur-1);
else
return recfind(searchkey,cur+1,high);
}
public void ins(long value)
{
int n=0;
a[num]=value;
num++;
}
public void insertion()
{
int in,out;
for( out=1;out<num ;out++)
{
long temp=a[out];
in=out;
while(in>0&&a[in-1]>temp)
{
a[in]=a[in-1];
--in;
}
a[in]=temp;
}
}
public void display()
{
for(int i=0;i<num;i++)
{
System.out.print(a[i]+" ");
System.out.println(" ");
}
}
}
递归二分查找和非递归二分查找有相同的大O效率:O( logN )
递归的二分查找是分治算法的一个例子,把大问题分成两个相对来说更小的问题,并且分别解决每一个小问题。对于每一个小问题再进一步分成两个更小的问题,并且解决他们。直到易于求解的基值情况就可。在二分查找中,就有两个这样对自身的递归调用,但是只有一个真正执行了,取决于值的大小。
5、汉诺塔问题
递归算法:
假设想要把所有的盘子从塔源S移动到目标塔座上D,有一个中介塔座为I,假设S上有n个盘子,算法如下:
1、从S移动包含上面的n-1个盘子的子树到塔座I上;
2、从塔座S移动剩余的盘子(最大的盘子)到塔座D上;
3、从塔座I移动子树到塔座D上。
package triangle;
public class Towers {
static int disks=3;
public static void main(String[] args) {
doTower(disks,'a','b','c');
}
public static void doTower(int top, char from ,char inter,char to)
{
if(top==1)
System.out.println("disk1 from "+ from +" to " +to);
else
{
doTower(top-1,from,to ,inter);//from->inter
System.out.println("disk" +top +" from "+from +" to "+ to);
doTower(top-1,inter,from,to); //inter->to
}
}
}
具体执行过程如下:
就这样吧~
200

被折叠的 条评论
为什么被折叠?



