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];
}
}
AJPFX关于通过索引获取最大值的思路的更多相关文章
- 运行期以索引获取tuple元素-C++14
在编译期很容易根据索引来获取对应位置的元素,因为 tuple 的帮助函数 std::get<N>(tp) 就能获取 tuple 中第 N 个元素.然而我们却不能直接在运行期通过变量来获取 ...
- 运行期以索引获取tuple元素-C++17
//运行期以索引获取tuple元素-C++17 //需支持C++17及以上标准的编译器,VS2017 15.5.x.CodeBlocks 16.01 gcc 7.2 //参见:http://purec ...
- 运行期以索引获取tuple元素-C++11之2
//运行期以索引获取tuple元素-C++11之2 //需支持C++11及以上标准的编译器,VS2017 15.5.x.CodeBlocks 16.01 gcc 7.2 //参见<深入应用C++ ...
- 运行期以索引获取tuple元素-C++11之1
//运行期以索引获取tuple元素-C++11之1 //需支持C++11及以上标准的编译器,VS2017 15.5.x.CodeBlocks 16.01 gcc 7.2 //参见<深入应用C++ ...
- 运行期以索引获取tuple元素-C++14(原创)
在编译期很容易根据索引来获取对应位置的元素,因为 tuple 的帮助函数 std::get<N>(tp) 就能获取 tuple 中第 N 个元素.然而我们却不能直接在运行期通过变量来获取 ...
- [Swift]字符串根据索引获取指定字符,依据ASCII实现字符和整数的相互转换
ASCII(American Standard Code for Information Interchange,美国信息交换标准代码)是基于拉丁字母的一套电脑编码系统,主要用于显示现代英语和其他西欧 ...
- IBOutletCollection 索引获取顺序问题
在sb中绑定了一个IBOutletCollection后,根据索引获取元素发现和自己拖线时的顺序不同,有时又会根据顺序,不知道是xcode的bug还是本身就是无序的. 在使用的时候直接排序: - (v ...
- jquery通过索引获取元素
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- mysql,字符串类型id,获取最大值
说明,这个id是字符串类型,但是实际值是一个整数,获取最大值的方法是: select max(cast(id as SIGNED)) from table 另外,mysql生成伪列的方法: SELEC ...
随机推荐
- Make 编译脚本上手
考察下面的示例代码: main.c #include <stdio.h> int main(){ printf("hello world!"); return 0; } ...
- 《鸟哥的Linux私房菜-基础学习篇(第三版)》(六)
第5章 首次登陆与在线求助man page 1. 首次登陆系统 首先谈了首次登陆CentOS 5.x界面.登陆选项中的会话是能够使用不同的图形界面来操作整个Linux系统. ...
- 嵌入式驱动开发之--- 虚拟磁盘SBULL块设备驱动程序分析
#define SBULL_MINORS 16 /* 每个sbull设备所支持的次设备号的数量 */ #define KERNEL_SECTOR_SIZE 512 // 本地定 ...
- [办公应用]从美国带回来的兄弟牌brother打印机如何处理
周末帮朋友去调一台国外带回来的brother一体机,型号MFC-J630W.这是她单位老板从国外带回来的,说是便宜:不过她说只有英文说明书,她不太会操作.我想这还不容易么,就满口答应下来了.我先到br ...
- Java中抽象类和接口的区别?
深入理解Java的接口和抽象类 对于面向对象编程来说,抽象是它的一大特征之一.在Java中,可以通过两种形式来体现OOP的抽象:接口和抽象类.这两者有太多相似的地方,又有太多不同的地方.很多人在初学的 ...
- 城域网IPv6过渡技术—NAT64+DNS64 Test for IPv6 DNS64/NAT64 Compatibility Regularly
城域网IPv6过渡技术—NAT64+DNS64 - 51CTO.COM http://network.51cto.com/art/201311/419623.htm Supporting IPv6 D ...
- Common non-standard response fields
https://en.wikipedia.org/wiki/List_of_HTTP_header_fields#cite_note-52 Common non-standard response f ...
- leetcode 395. Longest Substring with At Least K Repeating Characters
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- python利用决策树进行特征选择
python利用决策树进行特征选择(注释部分为绘图功能),最后输出特征排序: import numpy as np import tflearn from tflearn.layers.core im ...
- codeforces 689E E. Mike and Geometry Problem(组合数学)
题目链接: E. Mike and Geometry Problem time limit per test 3 seconds memory limit per test 256 megabytes ...