首页 > ACM题库 > HDU-杭电 > HDU 1040 As Easy As A+B-分治-[解题报告] C++
2013
11-26

HDU 1040 As Easy As A+B-分治-[解题报告] C++

As Easy As A+B

问题描述 :

These days, I am thinking about a question, how can I get a problem as easy as A+B? It is fairly difficulty to do such a thing. Of course, I got it after many waking nights.
Give you some integers, your task is to sort these number ascending (升序).
You should know how easy the problem is now!
Good luck!

输入:

Input contains multiple test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow. Each test case contains an integer N (1<=N<=1000 the number of integers to be sorted) and then N integers follow in the same line.
It is guarantied that all integers are in the range of 32-int.

输出:

For each case, print the sorting result, and one line one case.

样例输入:

2
3 2 1 3
9 1 4 7 2 5 8 3 6 9

样例输出:

1 2 3
1 2 3 4 5 6 7 8 9

/**

排序模板题: 快排是比较快速的算法~

分治的思想, 把每个元素放到合适的位置

*/

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cmath>
#include <vector>
#include <iomanip>
#include <algorithm>
//#include "myAlgorithm.h"
#define MAXL 100005
#define INF 1e8
using namespace std;
int n;
int a[MAXL];
int partion(int low, int high){
int key = a[low];
while(low < high){
while(low < high && key <= a[high]){
high--;
}
swap(a[low], a[high]);
while(low < high && key >= a[low]){
low ++;
}
swap(a[low], a[high]);
}
return low;
}

void quickSort(int low, int high){
if(low < high){
int mid = partion(low, high);
quickSort(low, mid -1);
quickSort(mid + 1, high);
}
}

int main()
{
///freopen("in.txt","w",stdout);
int k;
cin>>k;
while(k--){
cin>>n;
for( int i = 0; i < n; i++){
cin>>a[i];
}
quickSort(0, n - 1);
for( int i = 0; i < n - 1; i++){
cout<<a[i]<<" ";
}cout<<a[n-1]<<endl;
}
return 0;
}

  1. 题本身没错,但是HDOJ放题目的时候,前面有个题目解释了什么是XXX定律。
    这里直接放了这个题目,肯定没几个人明白是干啥