【HDU】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

AC代碼:

//
//  main.cpp
//  2016
//
//  Created by showlo on 2018/4/15.
//  Copyright © 2018年 showlo. All rights reserved.
//

#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define inf 100000

int main() {
    int a[110];
    int n,j;
    int temp;
    while (scanf("%d",&n)!=EOF&&n) {
        j=0;
        int min=inf;
        for (int i=0; i<n; i++) {
            scanf("%d",&a[i]);
            if (a[i]<min)
            {
                min=a[i];
                j=i;
            }
        }
        temp=a[j];
        a[j]=a[0];
        a[0]=temp;
        printf("%d",a[0]);
        for (int i=1; i<n; i++) {
            printf(" %d",a[i]);
        }
        printf("\n");
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章