zoukankan      html  css  js  c++  java
  • AJPFX关于通过索引获取最大值的思路

    /**
    * 通过索引获取最大值
    *
    *
    */
    public class Test1 {
            public static void main(String[] args) {
                    int[] arr = {11,22,33,44,55,66};
                    int max = getMax(arr);
                    System.out.println(max);
                    System.out.println("=======================");
                    int max2 = getMaxByIndex(arr);
                    System.out.println(max2);
                    
                    System.out.println("----------------------");
                    int max3 = getMax(arr, true);
                    System.out.println(max3);
                    System.out.println("**************************");
                    int max4 = getMax(arr,false);
                    System.out.println(max4);
            }
            //通过假定数组中的第一个元素是最大值,然后不断的进行判断,最终获取对大值
            public static int getMax(int[] arr){
                    int max = arr[0];//假设数组的第一个元素为最大值
                    for(int i=0;i<arr.length;i++) {
                            if(max<arr[i]) {
                                    max = arr[i];
                            }
                    }
                    return max;
            }
            /**
             * 此方法可以求出数组对应的最大值或者最小值
             * @param arr
             * @param flag :true:表示求最大值,false:表示求最小值
             * @return
             */
            public static int getMax(int[] arr,boolean flag){
                    int max = arr[0];//假设数组的第一个元素为最值
                    for(int i=0;i<arr.length;i++) {
                            if(flag) {
                                    if(max<arr[i]) {
                                            max = arr[i];
                                    }                                
                            } else {
                                    if(max>arr[i]) {
                                            max = arr[i];
                                    }
                            }
                    }
                    return max;
            }
            
            //通过获取最大值的角标,最终返回该角标对应的数值
            public static int getMaxByIndex(int[] arr){
                    int max = 0;//假设数组的角标为0的元素是最大的
                    for(int i=0;i<arr.length;i++) {
                            if(arr[max]<arr[i]) {
                                    max = i; //max中存储的是当前最大值所对应的角标
                            }
                    }
                    return arr[max];
            }
            
    }

  • 相关阅读:
    使用log4cplus时遇到的链接错误:无法解析的外部符号 "public: static class log4cplus::Logger __cdecl log4cplus::Logger::getInstance(class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,
    linux中free命令内存分析
    ImportError: libmysqlclient_r.so.16: cannot open shared object file: No such file or directory
    error LNK2038: 检测到“_ITERATOR_DEBUG_LEVEL”的不匹配项: 值“0”不匹配值“2
    创建预编译头 Debug 正常 Release Link Error:预编译头已存在,使用第一个 PCH
    C++定义字符数组
    客户端数据持久化解决方案: localStorage
    转:JavaScript函数式编程(三)
    转: JavaScript函数式编程(二)
    转:JavaScript函数式编程(一)
  • 原文地址:https://www.cnblogs.com/AJPFX/p/10816427.html
Copyright © 2011-2022 走看看