hdu - 1040 ACM

这个题目就是用O(logN)的排序就可以了~

我这里用了快排~

//
//  main.cpp
//  hdu_1040_2
//
//  Created by Alps on 15/1/3.
//  Copyright (c) 2015年 chen. All rights reserved.
//

#include <iostream>
using namespace std;

void swapint(int &a, int &b){
    int temp = a;
    a = b;
    b = temp;
}


void QuickSort(int *A, int left, int right){
    if (left < right) {
        int i = left, j = left;
        int mid = right;
        int temp;
        for (int k = left; k < right; k++) {
            if (A[j] <= A[mid]) {
                temp = A[i];
                A[i] = A[j];
                A[j] = temp;
                i++;
                j++;
            }else{
                j++;
            }
        }
        temp = A[i];
        A[i] = A[mid];
        A[mid] = temp;
        mid = i;
        QuickSort(A, left, mid-1);
        QuickSort(A, mid+1, right);
    }
    
}

int main(int argc, const char * argv[]) {
    int lineN = 0, caseN = 0;
    int str[1001];
    scanf("%d",&lineN);
    for (int i = 0; i < lineN; i++) {
        scanf("%d",&caseN);
        for (int j = 0; j < caseN; j++) {
            scanf("%d",&str[j]);
        }
        QuickSort(str, 0, caseN-1);
        for (int j = 0; j < caseN; j++) {
            printf("%d",str[j]);
            if (j == caseN-1) {
                printf("\n");
            }else{
                printf(" ");
            }
        }
    }
    return 0;
}


你可能感兴趣的:(HDU,1040)