当前位置:网站首页>Find the minimum value and location in multiple numbers (with repetition)
Find the minimum value and location in multiple numbers (with repetition)
2022-07-23 15:50:00 【Still work hard!】
Find the minimum value among the given numbers , And get the position of the minimum value in the number .
- When there are no duplicate values in multiple numbers , Just walk through the list that stores these numbers ;
- When there are duplicate values in multiple numbers , You can add a new list
MinIndexTo store the location of the current minimum , When the minimum value changes during traversal , It needs to be removedMinIndexAnd add the position of the new minimum value toMinIndexin , When there is a duplicate value of the minimum value in the traversal process , You need to fill in the position of this valueMinIndexin .
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++)
{
// If the minimum value changes , Then change the minimum value ,
// And clear the List, And add the position of the current value
if (MinNum > NumList[i])
{
MinNum = NumList[i];
MinIndex.Clear();
MinIndex.Add(i);
}
// If duplicate values occur , The position of the current value is also added to the position of the record minimum List
else if (MinNum == NumList[i])
{
MinIndex.Add(i);
}
}
// When the minimum value repeats , Record the position of the minimum value List There will be multiple numbers
// If only one minimum position is required , Yes, you can. List Sort at random , Then go to List[0] that will do
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;
}
}
边栏推荐
猜你喜欢
随机推荐
Start other independent programs through fmmonitoredprocess in unreal
《快速掌握QML》第四章 事件处理
printf函数-转换说明
在多个数字(有重复)中找到最小值以及所在位置
软件测试周刊(第81期):能够对抗消极的不是积极,而是专注;能够对抗焦虑的不是安慰,而是具体。
种种迹象表明,Apple将有望支持AV1
aws篇6 aws iot
C语言书籍推荐
day1
Modify SSH command line[ [email protected] ]Color
C语言经典例题-逆序打印输入的两位数
JSD-2204-会话管理-过滤器-Day19
Analysis of data governance
复现各种对抗攻击方法
Jsd-2204 session management filter day19
Six ways of uniapp route jump
MySQL execution order
[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~
2022最NB的JVM基础到调优笔记,吃透阿里P6小case
946. 验证栈序列 ●● & 剑指 Offer 31. 栈的压入、弹出序列 ●●









