• 递归简单程序题


    1、冒泡排序算法(递归)

     1 public class 冒泡排序 {
     2     public static void main(String args[]){
     3         int[] array = {4,3,56,73,25,6,37,8,1,46};
     4         int n = array.length;
     5         BubbleSort(array, n, 0);
     6         for(int i=0;i<n;i++){
     7             System.out.println(array[i]);
     8         }
     9     }
    10     
    11     private static void BubbleSort(int array[],int n,int i){
    12         if(i == n-1)    return;
    13         else{
    14             boolean exchange = false;
    15             for(int j=n-1;j>i;j--){
    16                 if(array[j] < array[j-1]){
    17                     int temp = array[j];
    18                     array[j] = array[j-1];
    19                     array[j-1] = temp;
    20                     exchange = true;
    21                 }
    22             }
    23             
    24             if(exchange==false)    return;
    25             else {
    26                 BubbleSort(array, n, i+1);
    27             }
    28         }
    29     }
    30 
    31 }
    冒泡排序

    2、斐波那契数

     1 public class 斐波那契数 {
     2     public static void main(String args[]){
     3         int n = 9;
     4         System.out.println(Fib(n));
     5     }
     6     
     7     private static int Fib(int n){
     8         if(n==1 || n==2){
     9             return 1;
    10         }else{
    11             return Fib(n-1)+Fib(n-2);
    12         }
    13     }
    14 
    15 }
    斐波那契数

    3、求数组中最大值

     1 public class 求数组中最大值 {
     2     public static void main(String args[]){
     3         int a[]={1,2,3,4,5};
     4         System.out.println(fmax(a, 5));
     5     }
     6     
     7     private static int fmax(int a[],int i){
     8         if(i==1){
     9             return a[0];
    10         }else{
    11             return Math.max(fmax(a, i-1),a[i-1]);
    12         }
    13     }
    14 
    15 }
    求数组中最大值

    4、整数逆序输出

     1 public class 整数逆序输出 {
     2     public static void main(String args[]){
     3         int a = 12345;
     4         digits(a);
     5     }
     6     
     7     private static void digits(int n){
     8         if(n!=0){
     9             System.out.print(n%10);
    10             digits(n/10);
    11         }
    12     }
    13 
    14 }
    整数逆序输出
  • 相关阅读:
    IPC——信号量
    IPC——命名管道
    IPC——匿名管道
    IPC——信号
    管道和命名管道
    Oracle业务用户密码过期问题的解决
    Oracle获取数据库中的对象创建语句
    RAC禁用DRM特性
    配置Server Side TAF
    同一环境下新建Standby RAC库
  • 原文地址:https://www.cnblogs.com/AIchangetheworld/p/12586444.html
Copyright © 2020-2023  润新知