• 用递归的方法求数组中的最大数


    思路: 
    得到全部5个中最大的数--> 比较第5个数与前四个中最大数的值-> 得到前四个中最大的数--> 比较第四个数与前三个中最大数的值-->得到前三个数中的最大值-->比较第三个数与前两个中最大数的值-->得到前两个数中最大的值-->比较第二个数与第一个数中的最大值

    但实际运算是从最右端往左端逐步(和上面的执行路径正好相反)比较的。

     1 package test;
     2 
     3 public class ArrayMax {
     4 
     5     public static void main(String[] args) {
     6         // TODO Auto-generated method stub
     7           int x[] = {10, -2, 4, 49, 49, 100, 23, 4};
     8           System.out.println("max:" +max(x, x.length));
     9     }
    10     
    11     static int max(int x[], int n)
    12     {
    13         if (n == 1)
    14         {
    15             return x[0];
    16         }
    17         else
    18         {
    19             if (x[n - 1] > max(x, n - 1))
    20             {
    21                 return x[n - 1];
    22             }
    23             else
    24             {
    25                 return max(x, n - 1);
    26             }
    27         }
    28     }
    29 
    30 }
  • 相关阅读:
    extjs数据类型
    Extjs 动态控制列显示
    400
    extjs主单清单同时编辑提交
    js-map模拟
    Leetcode 407.接雨水
    Leetcode 406.根据身高重建队列
    Leetcode 405.数字转化为十六进制数
    Leetcode 402.移掉k位数字
    Leetcode 401.二进制手表
  • 原文地址:https://www.cnblogs.com/isoftware/p/3798140.html
Copyright © 2020-2023  润新知