【发布时间】:2015-09-12 03:42:09
【问题描述】:
我目前在从数组中获取最大值时遇到问题,这是我的代码:
#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;
}
我想要做的是首先从一个数组中获取最大值,然后留下一个数组值,然后得到第二个最大值,第三个最大值相同,例如:
200
100
50
300
400
500
600
700
800
900
如果在上面的测试值中900是最大值,那么接下来的第二和第三个最大值应该是700和500,有什么办法吗?
【问题讨论】:
-
数组排序了吗?如果不是最简单的,那就对它进行排序,然后获取值。
-
为什么不排序?你可以取第 n 个最大值....?