当前位置:网站首页>在多个数字(有重复)中找到最小值以及所在位置
在多个数字(有重复)中找到最小值以及所在位置
2022-07-23 11:18:00 【还是要努力呀!】
在给定的多个数字中查找到最小值,并且获取最小值在数字中的位置。
- 当多个数字中不存在重复值时,仅需要遍历一遍存储这些数字的列表即可;
- 当多个数字中存在重复值时,可以新增一个列表
MinIndex来存储当前最小值的位置,在遍历过程中最小值出现变化时,需要清除MinIndex并将新的最小值的位置添加到MinIndex中,当遍历过程中出现最小值的重复值时,需要将该值的位置也填进MinIndex中。
public static List<int> FindMin(List<float> NumList)
{
List<int> MinIndex = new List<int>() {
0 };
float MinNum = NumList[0];
for (int i = 1; i < NumList.Count; i++)
{
//如果最小值出现变化,则更改最小值,
//并清除记录最小值位置的List,并将当前值的位置添加进去
if (MinNum > NumList[i])
{
MinNum = NumList[i];
MinIndex.Clear();
MinIndex.Add(i);
}
//如果出现重复值,也将当前值的位置添加到记录最小值位置的List
else if (MinNum == NumList[i])
{
MinIndex.Add(i);
}
}
//当最小值出现重复时,记录最小值位置的List中将会有多个数字
//如果只需要一个最小值位置,可以对该List进行随机排序,然后去List[0]即可
if (MinIndex.Count > 1)
{
System.Random rand = new System.Random();
List<int> RandList = new List<int>();
foreach (int i in MinIndex)
{
RandList.Insert(rand.Next(RandList.Count), i);
}
return RandList;
}
else
{
return MinIndex;
}
}
边栏推荐
猜你喜欢

Safety 7.18 operation
![[pyGame actual combat] aircraft shooting masterpiece: fierce battle in the universe is imminent... This super classic shooting game should also be taken out and restarted~](/img/a3/087b1bc7445c53ddbd6d7334da51b9.png)
[pyGame actual combat] aircraft shooting masterpiece: fierce battle in the universe is imminent... This super classic shooting game should also be taken out and restarted~
![[200 opencv routines] 225. Fourier descriptor for feature extraction](/img/4b/1f373505ffd5c0dbaa5c20431c4b42.png)
[200 opencv routines] 225. Fourier descriptor for feature extraction

VSCode 更新后与tab相关快捷键无法使用

Idea starts multiple projects at once

Error | cannot read property '_ normalized‘ of undefined

JSD-2204-会话管理-过滤器-Day19

《快速掌握QML》第五章 组件

Idea five free plug-ins to improve efficiency

Xlswriter - Excel export
随机推荐
Modify SSH command line[ [email protected] ]Color
C# 关闭当前电脑指令
String与Integer互相转换
修改ssh命令行[[email protected]]#颜色
Smart headline: smart clothing forum will be held on August 4, and the whole house smart sales will exceed 10billion in 2022
[pyGame actual combat] aircraft shooting masterpiece: fierce battle in the universe is imminent... This super classic shooting game should also be taken out and restarted~
Clickhouse, let the query fly!!!
查找论文源代码
BGP basic configuration
【攻防世界WEB】难度三星9分入门题(中):ics-05、easytornado
STL deque
Fileinputformat of MapReduce inputformat
(BFS)模板+例题(走迷宫,八数码)
STL deque
C语言注释的方法
Safety 7.18 operation
select......for update 语句的功能是什么? 会锁表还是锁行?
BGP federal experiment
Axure advanced
Error | cannot read property '_ normalized‘ of undefined