zoukankan      html  css  js  c++  java
  • 算法题:找出同一个序列中的最大值和最小值

    package arithmetic;
    
    /**
     * 同时找出一个序列中最大值和最小值
     * 分两种情况:(1)序列只有两个元素
     *          (2)序列有多个元素,有多个元素分别从序列的两端开始查找
     * @author SHI
     */
    public class MaxAndMin {
    
        public static void main(String[] args) {
            int[] a = { 122, 41, 4, 5, 7, 2, 89, 122, 34, 56 };
    
            int low = 0;
            int high = a.length - 1;
            int max = 0;
            int min = 0;
    
            // 序列就只有两个元素
            if (a[0] < a[high] && a.length == 2) {
                min = a[0];
                max = a[high];
            } else {
                min = a[high];
                max = a[max];
            }
    
            // 序列中有多个元素存在,思想:从序列的两端开始遍历
            while (++low <= --high) {
                if (a[low] <= a[high]) {
                    if (a[low] < min) {
                        min = a[low];
                    }
                    if (a[high] > max) {
                        max = a[high];
                    }
                } else {
                    if (a[low] > max) {
                        max = a[low];
                    }
                    if (a[high] < min) {
                        min = a[high];
                    }
                }
            }
            System.out.println(max + "  " + min);
        }
    }
  • 相关阅读:
    kafka其他记录
    kafka基础
    营销网络建设
    营销体制管理
    营销队伍建设
    营销组成
    Asp.net model 绑定原理
    Cannot change version of project facet Dynamic Web Module to 3.0.
    maven 编译插件
    mysql 配置文件 value
  • 原文地址:https://www.cnblogs.com/shi-blog/p/4254562.html
Copyright © 2011-2022 走看看