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]);
    
            }
    
    }
  • 相关阅读:
    快速排序法的C#实现
    SQL语句执行效率及分析(note)
    如何在C#中运行数学表达式字符串
    TSQL删除重复数据,保留一条
    C#对象序列化XML时报错:反射类型XXX时出错
    c#如何扩展类型的内置方法
    把数字转换成阿拉伯数字大写的程序
    使用C#格式化字符串
    Silverlight中自己定义实现的双击方法
    原来是这样:C#中new一个对象时,发生了什么事?
  • 原文地址:https://www.cnblogs.com/shaomg/p/3566524.html
Copyright © 2011-2022 走看看