zoukankan      html  css  js  c++  java
  • 递归简单程序题

    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 }
    整数逆序输出
  • 相关阅读:
    JavaScript自定义事件
    用Java构建一个简单的WebSocket聊天室
    PHP实现支付宝小程序用户授权的工具类
    jq ajax超时设置
    gulp使用笔记
    vue学习—组件的定义注册
    echarts设置线条粗细
    求js数组的最大值和最小值
    js删除数组中的 "NaN"
    jq方法(end)
  • 原文地址:https://www.cnblogs.com/techgy/p/12586444.html
Copyright © 2011-2022 走看看