Golang获取int数组里的最大值和下标
package main
import (
"fmt"
) func main() { //获取一个数组里最大值,并且拿到下标 //声明一个数组5个元素
var arr []int = [...]int {, , , ,}
//假设第一个元素是最大值,下标为0
maxVal := arr[]
maxIndex := for i := ; i < len(arr); i++ {
//从第二个 元素开始循环比较,如果发现有更大的,则交换
if maxVal < arr[i] {
maxVal = arr[i]
maxIndex = i
}
} fmt.Printf("maxVal=%v, maxIndex=%v", maxVal, maxIndex)
}
maxVal=, maxIndex=
Golang获取int数组里的最大值和下标的更多相关文章
- PHP里获取一维数组里的最大值和最小值
<?php $arr = ['10','100','50','90','2','5']; $min = min($arr); $max = max($arr); echo $min.PHP_EO ...
- C#获取一个数组中的最大值、最小值、平均值
C#获取一个数组中的最大值.最小值.平均值 1.给出一个数组 ,,,,,-,,,,}; 2.数组Array自带方法 本身是直接可以调用Min(),Max(),Average()方法来求出 最小值.最大 ...
- PHP获取以为数组中的最大值和最小值
1.PHP获取一维数组中的最大值 <?php $a=array('1','3','55','99'); $pos = array_search(max($a), $a); echo $a[$po ...
- js 数组里求最大值和最小值
// 数组里相邻两个数做比较 取满足条件的那个(以此类推) var arr = [1,3,4,5,6,7]; function Max(array){ var max = array[0]; for( ...
- 获取checkbox数组 里面的值
echo '<td class="text-left"><input name="tids[]" type="checkbox&q ...
- 常用的函数式接口_Supplier和常用的函数式接口Supplier接口练习_求数组中元素最大值
Supplier接口 package com.yang.Test.SupplierStudy; import java.util.function.Supplier; /** * 常用的函数式接口 * ...
- day5 数组对角线及最大值
1.输出M行M列数组方针,求对角线元素和#define M 5void fun(int xx[][M], int n)//n行n列{ int i = 0; int sum = 0; for (i = ...
- C# 之 将string数组转换到int数组并获取最大最小值
1.string 数组转换到 int 数组 " }; int[] output = Array.ConvertAll<string, int>(input, delegate(s ...
- JavaScript获取数组最小值和最大值的方法
本文实例讲述了JavaScript获取数组最小值和最大值的方法.分享给大家供大家参考.具体如下: ? 1 2 3 4 5 6 var arr = new Array(); arr[0] = 100; ...
随机推荐
- [LeetCode] 78. Subsets tag: backtracking
Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...
- [LeetCode] 876. Middle of the Linked List_Easy tag: Linked List ** slow, fast pointers
Given a non-empty, singly linked list with head node head, return a middle node of linked list. If t ...
- Go vs Erlang - 转
From http://zhang.hu/go-vs-erlang/ Go vs Erlang 因为 云巴 系统对高并发.低延迟的需求,我们对各个语言.平台做了很多的调研比较工作.这自然就包括致力于开 ...
- React对比Vue(02 绑定属性,图片引入,数组循环等对比)
import React, { Component } from 'react'; import girl from '../assets/images/1.jpg' //这个是全局的不要this.s ...
- cocos2d JS touch(触摸监听)-快速添加事件监听器到管理器
cc.eventManager.addListener({ event: cc.EventListener.TOUCH_ALL_AT_ONCE, onTouchesMoved: function (t ...
- cocos2d-x C++ iOS工程集成第三方支付宝支付功能
一.在支付宝开放平台下载支付宝SDK(https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.WWgVz8&tr ...
- MYSQL 5.7修改密码,登录问题
mysql5.7 关于密码问题 报错: ERROR 1862 (HY000): Your password has expired. To log in you must change it usin ...
- caffe编译环境的错误:..build_release/src/caffe/proto/caffe.pb.h:23:35: fatal error: google/protobuf/arena.h: 没有那个文件
在搭建caffe的环境时出现错误: .build_release/src/caffe/proto/caffe.pb.h:23:35: fatal error: google/protobuf/aren ...
- maven 知识
1. maven 环境配置 Maven 3.3 要求 JDK 1.7 或以上 Maven 3.2 要求 JDK 1.6 或以上 Maven 3.0/3.1 要求 JDK 1.5 或以上 2. ...
- SnmpTools配置
上网搜索了很多文档,但是snmptools一直没有配置好,原因就是64机器,网上的说法大多直接复制过来的,或者就没有考虑64位机器.经过仔细搜索和测试,一下是详细的配置过程: Index 安装 如果是 ...