题目1053:互换最大最小数
参考代码:
import java.util.*;
public class Main {
public static void main(String arg[]){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
Integer n =sc.nextInt();
List<Integer> num1 = new ArrayList<Integer>() ;
Integer max=0;
Integer min=9999;
for(int i=0;i<n;i++){
Integer tempNum = sc.nextInt();
if(tempNum>max) max =tempNum;
if(tempNum<min) min = tempNum;
num1.add(tempNum);
}
Integer indexofmax = num1.indexOf(max);
num1.set(num1.indexOf(min), max);
num1.set(indexofmax, min);
System.out.println(num1.toString().replace("[", "")
.replace("]", "")
.replace(",", ""));
}
}
}