在C#中查找特定索引的最小值 [英] Find the minimum value of particular index in C#

查看:73
本文介绍了在C#中查找特定索引的最小值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两个数组:count [],sum []。



I created two arrays : count[], sum[] .

int[] count = { 1, 5, 2, 7, 3, 7 };
int[] sum   =   { 3, 4, 5, 6, 7, 8 };
int maxValue = count.Max();
var result = Enumerable.Range(0, count.Count()).Where(i => count[i] == maxValue).ToArray();
          // It yields result=3,5





现在我需要输出为max(count)对应min(sum)..!

例如,max(count) )是7,它位于指数-3,5。现在从sum [],我需要返回索引,其中最小值在3,5之间。



如果它的方法基于LINQ可能会更好。



先谢谢..!



Now I need the output as max(count) corresponding min(sum).. !
For example, max(count) is 7 ,it located in index-3,5. Now from the sum[], I need to return the index, which has minimum value among 3,5.

It could be better if its method based on LINQ.

Thanks in Advance..!

推荐答案

这个怎么样?
int[] count = { 1, 5, 2, 7, 3, 7 };
int[] sum = { 3, 4, 5, 6, 7, 8 };
int maxValue = count.Max();
int minValue = count.Select((v,i)=>new{v,i}).Where(e=>e.v==maxValue).Min(e=>sum[e.i]);

干杯

Andi

Cheers
Andi


根据我的理解。 />
您可以尝试这个老式的程序:

From what I understood.
You can try this old fashioned program:
// Defining array values
int[] count = { 1, 5, 2, 7, 3, 7 };
int[] sum =   { 3, 4, 5, 6, 7, 8 };

int MaxValue, MinValue, Result;
int scan;

MaxValue= count[0];
MinValue= sum[0];
Result= 0;
for (scan=1; scan < count.length; scan++) {
    if (count[scan]==MaxValue) {
        if (sum[scan] < MinValue) {
            MinValue= sum[scan];
            Result= scan;
        }
    }
    if (count[scan] > MaxValue) {
        MaxValue= count[scan];
        MinValue= sum[scan];
        Result= scan;
    }
}
return result


这篇关于在C#中查找特定索引的最小值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆