数据的交换输出
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 154919 Accepted Submission(s): 57217
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 5
代码:import java.util.Scanner;
class Main{
public static void main(String []args) {
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int n=sc.nextInt();
int a[]=new int[n];
int min=100000;
if(n==0) {
System.exit(0);
}
else
{
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
}
int count=0;
for(int i=0;i<n;i++) {
if(a[i]<min) {
min=a[i];
count=i;
}
}
if(count!=0) {
a[count]=a[0];
a[0]=min;
}
for(int i=1;i<=n;i++) {
System.out.print(a[i-1]);
if(i!=n)
System.out.print(" ");
else
System.out.println();
}
}
}
}
}