HDU 2016 数据的交换输出

来源:互联网 发布:今天淘宝怎么进不去了 编辑:程序博客网 时间:2024/06/06 04:07
/*********************************Date:2016/05/23-By*Example:4 2 1 3 4*        5 5 4 3 2 1*Exp:要考虑到各种情况,边缘值;     默认最小值只有一个,否则按     最后一个出现的计算********************************/#include<iostream>using namespace std;int main(){    int str[105];    int n,idx,min;    while(cin >> n){        if(n==0)            return 0;        int i=0;        for(;i<n;i++){            cin>>str[i];            if(i==0)                min=str[i];            if(str[i]<=min){//必须要有等于号,否则会漏掉最小数在首位的情况                min=str[i];                idx=i;            }        }        //交换        str[idx] = str[0];        str[0] = min;        //输出        for(int i=0;i<n;i++){            if(i!=n-1)                cout<<str[i]<<" ";            else cout<<str[i];        }        cout<<endl;    }    return 0;}

0 0
原创粉丝点击