• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Just Fools.
♕Heart-broken is just a fool.
   首页    新随笔    联系   管理    订阅  订阅

HDU 1040 As Easy As A+B 快排

Problem Description
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
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.
 
Output
For each case, print the sorting result, and one line one case.

 

Sample Input
2
3 2 1 3
9 1 4 7 2 5 8 3 6 9
 
Sample Output
1 2 3
1 2 3 4 5 6 7 8 9
 
题目的意思就是排序
这道题目的标题很有意思,跟A+B一样简单,所以我愉快地再敲了一次快排。
很水的一道题,其实C++可以直接用cstdlib的内置快排,但是这题那么水我就自己敲了。
代码如下:
 1 #include<cstdio>
 2 #include<cmath>
 3 #include<cstring>
 4 #include<iostream>
 5 using namespace std;
 6 int n,a[100000];
 7 void qsort(int l,int r)
 8 {
 9     int i=l;
10     int j=r;
11     int x=a[(l+r)/2];
12     do{
13     while (a[i]<x)
14     i++;
15     while (a[j]>x)
16     j--;
17     if(j>=i)
18     {
19         int y=a[i];
20         a[i]=a[j];
21         a[j]=y;
22         i++;
23         j--;
24     }    }
25     while (i<j);
26     if( l<j)
27     qsort(l,j);
28     if(i<r)
29     qsort(i,r);
30     
31     
32 }
33 int main(){
34     int t;
35     scanf("%d",&t);
36     while(t--)
37     {
38       scanf("%d",&n);
39       for (int i=1;i<=n;++i)
40       {
41           scanf("%d",&a[i]);
42       }    
43       qsort(1,n);
44       for (int i=1;i<=n-1;++i)
45       {
46           printf("%d ",a[i]);
47       }
48       printf("%d\n",a[n]);
49     }
50     return 0;
51 }

 

 

posted @ 2016-02-03 16:09  ♕Heart-broken  阅读(151)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3