交叉排序

来源:互联网 发布:painter中文版mac 编辑:程序博客网 时间:2024/05/28 04:53

Think:
1知识点:sort()实现快速排序
2思考:通过两次快速排序实现交叉排序,分治思想

SDUT题目链接

以下为Accepted代码

#include <bits/stdc++.h>using namespace std;int a[104], b[104];int main(){    int n, i, tp1, tp2;    tp1 = tp2 = 0;    scanf("%d", &n);    for(i = 1; i <= n; i++){        if(i & 1)            scanf("%d", &a[tp1++]);        else            scanf("%d", &b[tp2++]);    }    sort(a, a+tp1);    sort(b, b+tp2, greater<int>());    int op1 = 0, op2 = 0;    for(i = 1; i <= n; i++){        if(i & 1)            printf("%d%c", a[op1++], i == n? '\n': ' ');        else            printf("%d%c", b[op2++], i == n? '\n': ' ');    }    return 0;}/***************************************************User name: Result: AcceptedTake time: 0msTake Memory: 244KBSubmit time: 2017-07-15 10:41:56****************************************************/
原创粉丝点击