7-2 删除序列的最大和最小值问题-hebust (25 分)
学者在进行数据统计的时候,为了避免极值的影响,通常会忽略掉最大值和最小值,然后对剩余元素进行统计,请编写程序完成去除极值的工作。
输入格式:
一行字符串,数字均为整数,之间使用空格分开(元素个数>=3)。
输出格式:
去除两端极值后的剩余元素,升序排列,之间使用空格分开
输入样例:
在这里给出一组输入。例如:
10 3 2 -1 5 3 4 3 0 3 2
输出样例:
在这里给出相应的输出。例如:
0 2 2 3 3 3 3 4 5
代码
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
List list = new ArrayList();
String str = scanner.nextLine();
String[] strr = str.split(" ");
for (String s : strr) {
// System.out.print(s+" \\");
list.add(Integer.valueOf(s));
}
Collections.sort(list);
// System.out.println(list);
// int min = (int) list.get(0);
// int max = (int) list.get(list.size() - 1);
// List minmax = new ArrayList();
// minmax.add(min);
// minmax.add(max);
//
// list.removeAll(minmax);
list.remove(0);
list.remove(list.size()-1);
for (int i = 0; i < list.size(); i++) {
if (i == 0) {
System.out.print(list.get(i));
} else {
System.out.print(" " + list.get(i));
}
}
}
}