hdu_problem_2016_数据的交换输出

/*
*
*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
*
*
*Author
*lcy
*
*
*Source
*C语言程序设计练习(三)
*
*
*Recommend
*lcy
*
*/
#include<iostream>
using namespace std;
int a[100];
int main() {
 int n, min, location;
 while (cin >> n) {
  if (n == 0) break;
  location = 0;
  for (int i = 0; i < n; i++) {
   cin >> a[i];
   if (i == 0) {
    min = a[0];
   }
   else {
    if (a[i] < min) {
     min = a[i];
     location = i;
    }
   }
  }
  swap(a[0], a[location]);
  cout << a[0];
  for (int i = 1; i < n; i++) {
   cout << " " << a[i];
  }
  cout << endl;
 }
 system("pause");
 return 0;
}

猜你喜欢

转载自blog.csdn.net/CoderMaximum/article/details/86521776