数据的交换输出

#include <cstdio>
#include <algorithm>
using namespace std;

int main()
{
	int n;
	while(scanf("%d", &n) && n != 0)
	{
		int a[n], cmin, cmin_index;
		for(int i = 0; i < n; ++ i)
		{
			scanf("%d", &a[i]);
		}
		
		cmin = a[0];
		cmin_index = 0;
		
		for(int i = 1; i < n; ++ i)
		{
			if(a[i] < cmin)
			{
				cmin = a[i];
				cmin_index = i;
			}
		}
		
		swap(a[cmin_index], a[0]);
		
		for(int i = 0; i < n; ++ i)
		{
			if(i)	printf(" %d", a[i]);
			else	printf("%d", a[i]);
		}
		printf("\n");
	}
	
	return 0;
} 

  

posted @ 2019-07-29 10:41  青衫客36  阅读(239)  评论(0编辑  收藏  举报