题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=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
直接上代码:
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<sstream>
#include<map>
#define endl "\n"
using namespace std;
int main(){
int n;
while(scanf("%d",&n)==1&&n){
int *a=new int[n];//动态分配内存空间,我以前用的少,以后要注意
for(int i=0;i<n;i++){
cin>>a[i];
}
int min=a[0];
int index=0;
for(int i=1;i<n;i++){
if(min>a[i]){
min=a[i];
index=i;
}
}
swap(a[0],a[index]);
// int temp;
// temp=a[0];
// a[0]=a[index];
// a[index]=temp;
int first=1;
for(int i=0;i<n;i++){
if(first){
cout<<a[i];
first=0;
}else{
cout<<" "<<a[i];
}
}
cout<<endl;
delete a;// 用完记得删除空间
}
return 0;
}