当前位置:网站首页>在多个数字(有重复)中找到最小值以及所在位置
在多个数字(有重复)中找到最小值以及所在位置
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;
}
}
边栏推荐
- The landing process of 800V high-voltage fast charging was accelerated, and Junsheng Electronics was designated for the 500million euro project
- VSCode 更新后与tab相关快捷键无法使用
- [cloud native] install MySQL and redis services in the docker environment
- BGP federal experiment
- day1
- Conda设置代理
- Find a specific number in an ordered array (binary search or half search)
- bgp基本配置
- 开源四足机器人 附设计图及代码「建议收藏」
- Error | cannot read property '_ normalized‘ of undefined
猜你喜欢

Jsd-2204 session management filter day19

printf函数-转换说明

【攻防世界WEB】难度三星9分入门题(下):shrine、lottery

Six ways of uniapp route jump

【运维】ssh tunneling 依靠ssh的22端口实现访问远程服务器的接口服务

uniapp路由跳转的六种方式

老照片上色——DeOldify快速上手

day14函数模块

The exclamation point of vscode +tab shortcut key cannot be used, and the solution to the problem of a-soul-live2d plug-in

The current situation and history of it migrant workers
随机推荐
深入理解L1、L2正则化
[200 opencv routines] 225. Fourier descriptor for feature extraction
Chapter 4 event handling of quick mastering QML
Conda设置代理
STL deque
地图附近名片流量主小程序开发
aws篇6 aws iot
老照片上色——DeOldify快速上手
Safe operation 7.22
The difference between cookies and sessions
Analysis of data governance
C语言经典例题-switch case语句转换日期格式
AWS篇1
Safety 7.18 operation
VSCode的感叹号+tab的快捷键不能用,以及A-SOUL-live2d插件出问题的解决方法
day1
VMware virtual machine download, installation and use tutorial
查找论文源代码
C语言经典例题-求最少数量钞票
Learning about patents