public static int[] maxMin(int a[]) {
int[] res = new int[2];
int len = a.length;
if (len <= 0) {
return res;
}
res[0] = res[1] = a[0];
if (len % 2 == 0) {
for (int i = 0; i < len - 1; i += 2) {
if (a[i] > a[i + 1]) {
int tem = a[i];
a[i] = a[i + 1];
a[i + 1] = tem;
}
}
for (int i = 0; i < len; i += 2) {
if (res[0] > a[i]) {
res[0] = a[i];
}
}
for (int i = 1; i < len; i += 2) {
if (res[1] < a[i]) {
res[1] = a[i];
}
}
} else {
for (int i = 0; i < len - 1 - 1; i += 2) {
if (a[i] > a[i + 1]) {
int tem = a[i];
a[i] = a[i + 1];
a[i + 1] = tem;
}
}
for (int i = 0; i < len - 1; i += 2) {
if (res[0] > a[i]) {
res[0] = a[i];
}
}
for (int i = 1; i < len - 1; i += 2) {
if (res[1] < a[i]) {
res[1] = a[i];
}
}
if (res[0] > a[len - 1]) {
res[0] = a[len - 1];
} else if (res[1] < a[len - 1]) {
res[1] = a[len - 1];
}
}
return res;
}

两两比较的方法,大的在右边,小的在左边,然后在分别找最大最小值,n是偶数奇数要注意。

求n的元素的最大最小值的更多相关文章

  1. leetcode:Minimum Path Sum(路线上元素和的最小值)【面试算法题】

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

  2. hdu 1856 求集合里元素的个数 输出最大的个数是多少

    求集合里元素的个数 输出最大的个数是多少 Sample Input41 23 45 61 641 23 45 67 8 Sample Output42 # include <iostream&g ...

  3. 求矩阵主对角线元素的和 Exercise08_02

    import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年12月 * 题目:求矩阵主对角线元素的和 * */ public class Exercis ...

  4. java编程基础篇---------> 编写一个程序,从键盘输入三个整数,求三个整数中的最小值。

    编写一个程序,从键盘输入三个整数,求三个整数中的最小值. 关键:声明变量temp   与各数值比较. package Exam01; import java.util.Scanner; public ...

  5. 删除列表中重复元素以及求list中元素个数

    Python 去除列表中重复的元素 来自比较容易记忆的是用内置的set l1 = ['b','c','d','b','c','a','a'] l2 = list(set(l1)) print l2 还 ...

  6. 常用的函数式接口_Supplier和常用的函数式接口Supplier接口练习_求数组中元素最大值

    Supplier接口 package com.yang.Test.SupplierStudy; import java.util.function.Supplier; /** * 常用的函数式接口 * ...

  7. 【RMQ问题】求数组区间最大值,NYOJ-1185-最大最小值

    转自:http://blog.csdn.net/lilongherolilong/article/details/6624390 先挖好坑,明天该去郑轻找虐 RMQ(Range Minimum/Max ...

  8. JS基础:求一组数中的最大最小值,以及所在位置

    var arr = [0, 5, -3, 6, 2, -6, 10]; //定义一个最大值和一个最小值,把他们的索引值赋值给固定的两个变量 var maxValue = arr[0]; var min ...

  9. max_element(C++)求数组最大元素

    #include<iostream> #include<vector> #include<algorithm> using namespace std; int m ...

随机推荐

  1. Jmeter实例

    我们在性能测试过程中,首先应该去设计测试场景,模拟真实业务发生的情境,然后针对这些场景去设计测试脚本.为了暴露出性能问题,要尽可能的去模拟被测对象可能存在瓶颈的测试场景. 我在本地部署了一个项目,可以 ...

  2. iview 怎样屏蔽掉账户框自动显示账户名和密码(root,***)

    用iview框架做出的登录页面,账户名和密码显示框,会自动有占位信息(root,****) 后来解决问题发现,只要在真正的输入框下面添加这样的一行隐藏的代码,占位信息会自动填充到隐藏的input框内, ...

  3. C语言实现全排列和回溯法总结

    一.递归实现全排列 #include"cstdio" ]; void print_permutation(int n,int *A,int cur){ if(cur==n){ ;i ...

  4. Wamp设置虚拟目录

    1. 默认安装 wamp后,工作目录为"..../wamp/www" 也就是PHP文件只有放在此目录下才能打得开,打开Apache的配置文件httpd.conf可以看到: 这么两行 ...

  5. Download link in response body does not work--待解决

    I am having a problem similar to the ones described in #374 and #1196. I have a service which return ...

  6. 【request获取用户请求ip】

    1:request.getRemoteAddr() 2:如果请求的客户端使用了nginx 等反向代理发送请求的时候:就不能获取到真是的ip地址了:如:将http://192.168.1.110:204 ...

  7. 特殊引用类型(string)

    private string FuncWithParameters(int param1, String param2, List<string> param3) { // 我们在这里改变 ...

  8. 文档转换为pdf格式帮助类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using Word = M ...

  9. 在ASP.NET CORE中启用favicon.ico

    在静态页面中添加网站标志只需在<head>标签中添加<link rel="shortcut icon" href="favicon.ico" ...

  10. office2007安装时显示安装程序找不到 office.zh-cn\officeLR.cab怎么办

    根本原因是和VS2008有关解决方法如下:1. 找到vs2008安装程序(光盘,镜像文件,解压文件都一样),找到WCU文件夹在他里面找到WebDesignerCore文件夹,然后打开它找到WebDes ...