5
1 2 3 4 5
Sample Output
5 1 4 2 3
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class PracticeV {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n,f,val;
List<Integer> list;
while(sc.hasNext()){
n = sc.nextInt();
list = new ArrayList<Integer>();
f = 1;
for(int i=0; i<n; i++){
list.add(sc.nextInt());
}
while(list.size()>0){
if(f%2==0){ //求最小的数
val = getMax(list);
}else{ //求最大的数
val = getMin(list);
}
if(list.size()>1)
System.out.print(list.get(val)+" ");
else
System.out.print(list.get(val));
if(list.size()>0)
list.remove(val);
f++;
}
System.out.println();
}
}
private static int getMin(List<Integer> list) {
int max = 0;
for(int i=1; i<list.size(); i++){
if(list.get(i)>list.get(max)){
max = i;
}
}
return max;
}
private static int getMax(List<Integer> list) {
int min = 0;
for(int i=1; i<list.size(); i++){
if(list.get(i)<list.get(min)){
min = i;
}
}
return min;
}
}
又是内存超出限制....java内存优化很头疼...项目满足需求,但是内存运行就飚到了35兆...