如何留下一个阵列值之后获得最大的价值? [英] How to get largest value after leaving one array value?

查看:171
本文介绍了如何留下一个阵列值之后获得最大的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有问题,从一个数组获得最大的价值,这是我的code:

i am currently having problem getting largest values from an array, here is my code:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    int a[10],max=0,j,secondbig=0;
    for(int i=0;i<10;i++)
        {
        cin>>a[i];

    }
    max = a[0];
    for(int i=0;i<10;i++)
        {
        if(a[i]>max){
            max=a[i];
            j=i;
        }
    }
    secondbig=a[10-j-1];
    for(int i=0;i<10;i++)
        {
        if(secondbig <a[i] && j != i)
          secondbig =a[i];
    }
    cout<<max<<"\n"<<secondbig;
    return 0;
}

我想要做的是第一个获得最大的价值,从一个数组,然后留一个数组值,然后得到第二大的价值,同时为第三大值,例如:

What i want to do is to first get maximum value from an array and then leave one array value and then get second largest value and same for third largest value, for example :

200
100
50
300
400
500
600
700
800
900

如果在上面的测试值900是最大的价值,那么随后的第二和第三大值应该是700和500,反正是有这样做吗?

If in the above test values 900 is the largest value then the subsequent second and third largest value should be 700 and 500, is there anyway to do that?

推荐答案

根据你后续的问题,如果要列出所有的最大值,改变J&LT; 3至J&LT; 10。如果你想,你从阵列已提取的最大值的数字之和,你需要添加更多的变量:

As per your follow on question, If you want to list all the max values, change the j<3 to j<10. If you want the numeric sum of the max values that you have extracted from the array, you need to add one more variable:

int a[10], total, max, prior_max = numeric_limits<int>::max();

那么外环,发生在内环这条语句的结束之内:

then within the outer loop, place at the end of the inner loop this statement:

total += max;

再经过循环的执行完成后,就可以打印出全部或以其他方式使用。如:

Then after the execution of the loops are complete, you can print out the total or otherwise use it. as in:

cout << 'Total: ' << total << endl;

这篇关于如何留下一个阵列值之后获得最大的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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