如何获得数组中第二大值的索引。 [英] How To Get The Index Of 2nd Largest Value In The Array.

查看:84
本文介绍了如何获得数组中第二大值的索引。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的成员,我正试图找到第一个最大值和第二个最大值以及他们的指数太多......

到目前为止,我只是得到了最大的&指数和第二大..现在我在这里,因为我很困惑如何获得第二大价值指数.. :(



我如何设法获得第二大价值指数..



感谢您的考虑。



- > ;(此处代码) - >(cpp);

Respected members, I'm trying to find the First Maximum value and Second Maximum value and their Index Too ..
up till now, i'm just getting the largest & index and 2nd largest.. Now I'm here as i'm confused about how to get the index for the 2nd largest value .. :(

How do i manage to get index of the 2nd largest value ..

Thanks For Your Consideration.

->(code here)->(cpp);

#include <iostream>
using namespace std;


void setData(int a[],int size) {

	for(int i=0;i<size;i++) {
		cout <<"Enter element " << i+1 <<": ";
		cin >> a[i];
	}
}
void printData(int a[],int size) {

	for(int i=0;i<size;i++) {

		cout << a[i]<< endl;
	}
}
void foxi(int a[],int sizi) {

	int largest = a[0];
	int index, index2;
	int largest2 = a[0];
	for (int i=0;i<sizi;i++)
    {
        if(largest <= a[i])
        {
            largest2 = largest;
            index2 = i;
            largest = a[i];
            index = i;
        }
        else if (a[i] > largest2 && a[i] != largest)
        {
             largest2 = a[i];
        }
    }
    cout<<"largest = "<<largest << endl << " Index is = "<<index << endl
        << "largest2 = " << largest2<<" Index is = " <<index2-1 ;

	cout<<endl;
}


int main ()
{
	const int sizi=4;
	int a[sizi];

	setData(a,sizi);
	printData(a,sizi);
	cout << "\n";

	foxi(a, sizi);
	return 0;
}

推荐答案

尝试

Try
    // initialize both indices to zero
    int index = 0, index2 = 0;

    for (int i=0;i<sizi;i++)>
{
    if(a[i] > largest)
    {
        largest2 = largest;
        // ensure you also don't lose the index of the largest so far
        index2 = index;

        largest = a[i];
        index = i;
    }
    else if (a[i] >= largest2 && index != i) // use >= instead of >
    {
         largest2 = a[i];
         // keep track of the second best index
         index2 = i;
    }
}





希望这有助于帮助:)



BCD



Hope this Helps :)

BCD


int[] sorted_data = original_data;
Array.Sort(sorted_data);
int secondIndex = Array.IndexOf(original_data, sorted_data[1]);


如果您不介意订购,也可以使用partial_sort方法。 http://www.cplusplus.com/reference/algorithm/partial_sort/ [ ^ ]



虽然对于2项,但它可能有点过分,如果你想要说10个最大的项目,使用标准(STL)算法可能是个好主意。



否则解决方案1是合适的,您可以稍微修改它,例如,如果您希望在所有值都是最小可能值时正确处理该情况,因为该算法在这种情况下将返回索引0两次。
You can also use partial_sort method if you don't mind about ordering. http://www.cplusplus.com/reference/algorithm/partial_sort/[^]

Although for 2 item, it might be overkill, if you want say the 10 biggest items, it might be a good idea to use standard (STL) algorithms.

Otherwise solution 1 is appropriate and you might modify it slightly if for example, you want to properly handle the case when all values are the minimal possible value as that algorithm would return index 0 twice in such case.


这篇关于如何获得数组中第二大值的索引。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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