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]);
    
            }
    
    }
  • 相关阅读:
    字符串练习题
    js
    百度商桥--提供网站与用户之间交流平台
    git从本地上传到码云
    命名单词
    swiper 点击切换,拖动切换后继续自动轮播
    ionic4创建新项目
    两个年月日相减,获取年数和年数及半年数
    微信小程序点击跳转出现背景
    列表数据进行左浮动造成页面空白一块,排版错位问题
  • 原文地址:https://www.cnblogs.com/shaomg/p/3566524.html
Copyright © 2011-2022 走看看