Find the sub max value of an array[查找给定数组的次大值]

Posted by Darren Blog on October 24, 2020
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int subMax(int a[],int n)
{
  int max=a[0];
  int sub_max=a[0];
 
  for (int i = 1; i < n; i++)
  {
    if(a[i]>max)
    {
      //update max and sub max
      sub_max=max;
      max=a[i];
    }
    else if(a[i]<max)
    {
      if(a[i]>sub_max)
      {//update sub max
        sub_max=a[i];
      }
    }
    else
    {
      //do nothing
    }
  }
 
  return sub_max;
}

In spite of the strong wind,life never gives up.

纵有疾风起,人生不言弃。