Problem Description
输入n(n<100)个数,找出其中最小的数,将它与最前面的数交换后输出这些数。
Input
输入数据有多组,每组占一行,每行的开始是一个整数n,表示这个测试实例的数值的个数,跟着就是n个整数。n=0表示输入的结束,不做处理。
Output
对于每组输入数据,输出交换后的数列,每组输出占一行。
Sample Input
4 2 1 3 4 5 5 4 3 2 1 0
Sample Output
1 2 3 4 1 4 3 2 5import java.util.Scanner; public class POJ2016_ChangeNumber { public static void main(String[] args) { Scanner cin = new Scanner(System.in); //输入长度为1则退出 while (cin.hasNextLine()) { String b = cin.nextLine(); if(b.length() != 1) { new ChangeNumber(b); } else if (b.length() == 1 && Integer.parseInt(b) == 0) { break; } } } } class ChangeNumber { public ChangeNumber(String a) { String[] c = a.split(" "); //使用Integer包装类来将字符串转换成整数 int t = Integer.parseInt(c[1]); int locate = 1; for(int i = 2; i < c.length; i++) { if (Integer.parseInt(c[i]) < t) { t = Integer.parseInt(c[i]); locate = i; } if (i == c.length - 1) { c[locate] = c[1]; c[1] = String.valueOf(t); } } //输出字符串数组 for(int i = 1; i < c.length; i ++) { if (i != c.length - 1) { System.out.print(c[i] + " "); } else { System.out.println(c[i]); } } } }