zoukankan      html  css  js  c++  java
  • 杭电 看归并排序和快速排序

    shǎ崽 OrOrOrOrz

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3358    Accepted Submission(s): 1060
     
    Problem Description
    Acmer in HDU-ACM team are ambitious, especially shǎ崽, he can spend time in Internet bar doing problems overnight. So many girls want to meet and Orz him. But Orz him is not that easy.You must solve this problem first. The problem is : Give you a sequence of distinct integers, choose numbers as following : first choose the biggest, then smallest, then second biggest, second smallest etc. Until all the numbers was chosen . For example, give you 1 2 3 4 5, you should output 5 1 4 2 3
     
    Input
    There are multiple test cases, each case begins with one integer N(1 <= N <= 10000), following N distinct integers.
     
    Output
    Output a sequence of distinct integers described above.
     
    Sample Input
    5
    1 2 3 4 5
     
    Sample Output
    5 1 4 2 3
     
    Author
    WhereIsHeroFrom
    #include <stdio.h>
    
    void sqrot(int *a,int l,int r)
    {
        int i=l,j=r;
        int x=a[l];
        while(i<j)
        {
            while(i<j&&a[j]>=x)
                j--;
            if(i<j)
                a[i++]=a[j];
            while(i<j&&a[i]<=x)
                i++;
            if(i<j)
                a[j--]=a[i];
        }
        a[i]=x;
        if(l<r)
        {
        sqrot(a,l,i-1);
        sqrot(a,i+1,r);
        }
        
    }
    
    int a[10001];
    int main()
    {
        int n,j;
    
      while(scanf("%d",&n)!=EOF)
      {
        
            for(int i=0;i<n;i++)
                scanf("%d",&a[i]);
            sqrot(a,0,n-1);
    
            for(j=n-1,i=0;(j-i)>=2;i++,j--)
                printf("%d %d ",a[j],a[i]);
            if(j-i==0)
                printf("%d
    ",a[i]);
            if(j-i==1)
                printf("%d %d
    ",a[j],a[i]);
        }
    
    return 0;
    }
    #include<stdio.h>
    
    void mergearry(int *a,int first,int mid,int last,int *temp){
    
        int i=first,m=mid,j=mid+1,n=last,k=0;
        while(i<=m&&j<=n){
            if(a[i]<=a[j])
                temp[k++]=a[i++];
            else
                temp[k++]=a[j++];
        }
        while(i<=m)
            temp[k++]=a[i++];
        while(j<=n)
            temp[k++]=a[j++];
        for(i=0;i<k;i++)
            a[first+i]=temp[i];
    
    }
    
    
    
    
    void mergesort(int *a,int first,int last,int *temp){
        if(first<last)                                    
        {int mid=(first+last)/2;
        mergesort(a,first,mid,temp);                      
        mergesort(a,mid+1,last,temp);
        mergearry(a,first,mid,last,temp);
    
        }
    }
    
    
    
    
    int main(){
            int n,j;
            while(scanf("%d",&n)!=EOF){
            int a[10001];
            int b[10001];
    
            for(int i=0;i<n;i++)
                scanf("%d",&a[i]);
            mergesort(a,0,n-1,b);
            for(j=n-1,i=0;(j-i)>=2;i++,j--)
                printf("%d %d ",a[j],a[i]);
            if(j-i==0)
                printf("%d
    ",a[i]);
            if(j-i==1)
                printf("%d %d
    ",a[j],a[i]);
    
            }
    
    }
  • 相关阅读:
    Jenkins联动码云自动匹配分支进行构建流水线
    SpringBoot集成MyBatis的分页插件PageHelper--详细步骤
    java服务端集成极光消息推送--详细开发步骤
    深入理解jvm--性能监控工具
    深入理解jvm--分代回收算法通俗理解
    intellij IDEA github clone 指定分支代码
    java程序员常用的cmd命令
    【推荐】我凭这三招轻松拿到offer(吐血整理)
    java系统化基础-day02-运算符、选择结构、循环结构
    linux上安装redis-单机版
  • 原文地址:https://www.cnblogs.com/shaomg/p/3566524.html
Copyright © 2011-2022 走看看