2017百度编程第一题 求倒数第三便宜
AC完成
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class baidu {
public static void main(String[] args)
{
Scanner scanner=new Scanner(System.in);
while (scanner.hasNext()) {
int n=scanner.nextInt();
int[] num=new int[n];
for (int i = 0; i < n; i++) {
num[i]=scanner.nextInt();
}
System.out.println(sanMax(num));
}
}
private static int sanMax(int[] num) {
int temp=-1;
Arrays.sort(num);//偷懒了,直接进行排序
int max=num[0];
int count=1;
for (int i = 1; i < num.length; i++) {
if (max<num[i]&&count<=2) {//通过比较并计数
count++;
max=num[i];
}
}
if (count==3) {
return max;
}
else {
return temp;
}
}
}