「java递归例题」递归java案例

博主:adminadmin 2022-12-04 22:48:07 67

本篇文章给大家谈谈java递归例题,以及递归java案例对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

Java 递归题目,计算1-1/2+1/3-1/4...(-1)^(n+1) *1/n 要用递归,不要用循环

public double foo(int n){

if(n == 1)

return 1.0;

if(n % 2 == 0)

return -1.0/n + foo(n - 1);

else

return 1.0/n + foo(n-1);

}

思想就是这么个思想,说要计算分数相加,这块就自己解决吧

java中递归的应用!!!f(20)=1,f(21)=4,f(n+2)=2*f(n+1)+f(n); 其中,n是大于零的整数,求f(10)的值。

public class Test {

public static int f(int n){

if(n==20){

return 1;

}else if(n==21){

return 4;

}else if(n20){

return f(n+2)-2*f(n+1);

}else{

return 2*f(n-1)+f(n-2);

}

}

public static void main(String[] args) {

System.out.println(f(10));//求出f(10)的值

}

}

已经通过测试,在main函数中输入f(n),其中n为自己手动调整,就能求出值

JAVA这道题要如何用递归实现呢,求大神

按照你的要求编写的Java递归程序如下:

import java.util.Scanner;

public class GGG {

 public static void main(String[] args) {

  int N = 0;

  Scanner sc=new Scanner(System.in);

  int num=sc.nextInt();

  for(int n=0;nnum;n++){

   N=sc.nextInt();

   int a[]=new int[N];

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

    a[i]=sc.nextInt();

   }

   System.out.print("case "+(n+1)+":");

   process(a,0);

   System.out.println();

  }

 }

 private static void process(int[] a, int n) {

  if(n==0){

   if(isPrime(a[n+1]))

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

   else

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

   

  }else if(n==a.length-1){

   if(isPrime(a[n-1]))

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

   else

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

   return;

  }else{

   if(isPrime(a[n-1])isPrime(a[n+1]))

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

   else if(isPrime(a[n-1])||isPrime(a[n+1]))

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

   else

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

  }

  process(a,n+1);

 }

 public static boolean isPrime(int num) {

  int i;

  for(i=2;inum;i++){

            if(num%i==0)

            break;

         }

       if(i==num){

        return true;

       }

       return false;

 }

}

运行结果:

2

5

5 7 2 9 13

case 1:1 2 1 2 0

3

10 4 5

case 2:0 1 0

Java 一个简单的递归问题

就是这个意思 ,递归就是不断的调用方法自身,直到满足递归停止条件,在这个里面就是

if(n==1){

return 2;

}

第二个问题的思路

1:f4+f3

2:(f3+f2)+(f2+f1)

3: ( (f2+f1)+f2)+(f2+f1)

结果为 1+1+1+1+1=5

java递归算法的例子。

阶乘:

要求:给定一个数值,计算出它的阶乘值,例如5的阶乘为5*4*3*2*1

实现:

[html] view plaincopy

span style="font-size:12px;"  // 利用递归实现一个数的阶乘值      private static BigDecimal getNum(BigDecimal inNum) {          if (inNum.compareTo(BigDecimal.ONE) == 0) {              return inNum;          }          return inNum.multiply(getNum(inNum.subtract(BigDecimal.ONE)));      }/span

(2)Fibonacci数列:1,1,2,3,5,8,13……

要求:找出数列中指定index位置的数值

实现:

[html] view plaincopy

span style="font-size:12px;"  // 利用递归实现了Fibonacci数列      private static int fab(int index) {          if (index == 1 || index == 2) {              return 1;          } else {              return fab(index - 1) + fab(index - 2);          }      }/span

(3)汉诺塔

要求:汉诺塔挪动

实现:

[html] view plaincopy

span style="font-size:12px;"  span style="white-space:pre;" /spanprivate static final String DISK_B = "diskB";    span style="white-space:pre;"   /spanprivate static final String DISK_C = "diskC";    span style="white-space:pre;"   /spanprivate static final String DISK_A = "diskA";    span style="white-space:pre;"   /spanstatic String from=DISK_A;  span style="white-space:pre;" /span  static String to=DISK_C;  span style="white-space:pre;" /span  static String mid=DISK_B;    span style="white-space:pre;" /span  public static void main(String[] args) {  span style="white-space:pre;" /span      String input=JOptionPane.showInputDialog("please input the number of the disks you want me move.");  span style="white-space:pre;" /span      int num=Integer.parseInt(input);  span style="white-space:pre;" /span      move(num,from,mid,to);  span style="white-space:pre;" /span  }/span

[html] view plaincopy

span style="font-size:12px;"  // 利用递归实现汉诺塔      private static void move(int num, String from2, String mid2, String to2) {          if (num == 1) {              System.out.println("move disk 1 from " + from2 + " to " + to2);          } else {              move(num - 1, from2, to2, mid2);              System.out.println("move disk " + num + " from " + from2 + " to " + to2);              move(num - 1, mid2, from2, to2);          }      }/span

(4)排列组合

要求:将输入的一个字符串中的所有元素进行排序并输出,例如:你给出的参数是"abc",

则程序会输出

abc

acb

bac

bca

cab

cba

实现:

[html] view plaincopy

span style="font-size:12px;"span style="white-space:pre;"   /spanpublic static void permute(String str) {   span style="white-space:pre;"    /span   char[] strArray = str.toCharArray();    span style="white-space:pre;"   /span permute(strArray, 0, strArray.length - 1);  span style="white-space:pre;" /span}/span

[html] view plaincopy

span style="font-size:12px;"  // 利用递归实现,将输入的一个字符串中的所有元素进行排序并输出      public static void permute(char[] list, int low, int high) {          int i;          if (low == high) {              String cout = "";              for (i = 0; i = high; i++) {                  cout += list[i];              }              System.out.println(cout);          } else {              for (i = low; i = high; i++) {                  char temp = list[low];                  list[low] = list[i];                  list[i] = temp;                  permute(list, low + 1, high);                  temp = list[low];

关于java递归例题和递归java案例的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-04,除非注明,否则均为首码项目网原创文章,转载请注明出处。