一
1 int s_max = 0; 2 3 for (int i = 0; i < ar.Length; i++) 4 5 { 6 7 for (int j = 0; j <ar.Length; j++) 8 9 { 10 11 if (ar[j] > ar[i]) 12 13 { 14 15 int temp = ar[i]; 16 17 ar[i] = ar[j]; 18 19 ar[j] = temp; 20 21 } 22 23 } 24 25 } 26 27 if (ar.Length >= 2) 28 29 { 30 31 s_max= ar[ar.Length - 2]; 32 33 }
二
1 for (int i = 0; i < ar.Length; i++) 2 { 3 4 if (ar[i] > maxnumber) 5 { 6 7 s_max = maxnumber; 8 9 maxnumber = ar[i]; 10 11 } 12 13 else 14 { 15 16 if (ar[i] > s_max) 17 18 s_max = ar[i];//第二大 19 20 } 21 }