全国中小学生Scratch作品大赛拉开了序幕。每个参赛选手可以通过网络直接上传作品。本次比赛人人可做评委。每个网络评委可以通过网络对每一件作品进行打分。评分系统也是请程序高手设计的,能自动去掉一个最高分和一个最低分,求出平均分。
输入格式:
输入数据包括两行: 第一行为n,表示n个评委,n>2。 第二行是n个评委的打分,分数之间有一个空格。打分是可以带有小数部分的。
输出格式:
输出平均分,结果保留两位小数。
输入样例:
6
10 9 8 7.9 9 9.5
输出样例:
8.88
方法一:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
double sum = 0;
double maxx = 0;
double minn = 101001;
in.nextLine();
// 输入回车符
String line = in.nextLine();
// 一次性读入一串,加快运行效率
String f[] = line.split(" ");
for (int i = 0;i < n; i ++ ) {
double x = Double.parseDouble(f[i]);
if(maxx < x) maxx = x;
if(minn > x) minn = x;
sum += x;
}
System.out.printf("%.2f",(sum - maxx - minn) / (n - 2));
}
}
方法二:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
in.nextLine();//接受空白符
double sum = 0;
List<Double> nums = new ArrayList<>();//顺序表
String[] line = in.nextLine().split(" ");//以“ ”分隔分别放在这个String类型的数组中
for (String str : line) {//遍历
nums.add(Double.valueOf(str));//存入顺序表
}
Collections.sort(nums);//排序
nums.remove(0);//删除
nums.remove(nums.size() - 1);
for (double num : nums) {
sum += num;
}
System.out.println(String.format("%.2f", sum / (n - 2)));
}
}