排序练习——找出前m大的数字

来源:互联网 发布:fireworks cs6 mac 编辑:程序博客网 时间:2024/05/03 05:21

排序练习——找出前m大的数字

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

给定n个数字,找出前m大的数字。
 

输入

 多组输入,每组输入格式如下。
第一行包含两个整数n m。(n<=100000, m>0)
第二行包含n个正整数。

输出

 输出前m大的数字,若m>n输出ERROR。每组输出占一行。

示例输入

2 14 34 21 2 898989 23

示例输出

4898989 23基数排序 
#include <cstdio>#include <cmath>#include <cstring>#include <cstdlib>#include <iostream>using namespace std;const int MAX=110000;int Arr[MAX];int Getdigit(int x,int d){    return (x/d)%10;//计算每位的数字}void MSD_sort(int low,int high,int d){    int count[10]= {0};    int *b=new int[high-low+1];//为所有桶开辟空间    int sum=1;    for(int k=1;k<=d;k++)    {        memset(count,0,sizeof(count));        for(int i=low;i<=high;i++)        {            count[Getdigit(Arr[i],sum)]++;//统计数据的数量        }        for(int i=1;i<=9;i++)        {            count[i]+=count[i-1];//设置每个桶的右边界索引        }        //讲数据装入桶中        for(int i=high;i>=low;i--)//从右向左,保证数据的稳定性        {            int ans=Getdigit(Arr[i],sum);//计算关键码的数字;            b[count[ans]-1]=Arr[i];//放入对应的桶中,aount[ans]-1为右边界索引            --count[ans];        }        //收集数据        for(int i=low,j=0;i<=high;i++,j++)        {            Arr[i]=b[j];        }        sum*=10;    }    delete b;//删除}int main(){    int n,m;    while(~scanf("%d %d",&n,&m ))    {        for(int i=0; i<n; i++)        {            scanf("%d",&Arr[i]);        }        if(m>n)        {            cout<<"ERROR"<<endl;            continue;        }        MSD_sort(0,n-1,9);        for(int i=n-1; i>=(n-m); i--)        {            if(i!=n-1)                printf(" ");            printf("%d",Arr[i]);        }        printf("\n");    }    return 0;}<pre name="code" class="cpp">#include <bits/stdc++.h>#define RR freopen("input.txt","r",stdin)#define WW freopen("output.txt","w",stdout)#define ClearAll(A,T) memset(A,T,sizeof(A))/*手敲快排*/using namespace std;const int Max=110000;int Arr[Max];void Qsort(int low,int high){    int i=low,j=high,s=Arr[low];    if(i>=j)        return ;    while(i<j)    {        while(i<j&&Arr[j]<=s)            j--;        Arr[i]=Arr[j];        while(i<j&&Arr[i]>=s)            i++;        Arr[j]=Arr[i];    }    Arr[i]=s;    Qsort(low,i-1);    Qsort(i+1,high);}int main(){    int n,m;    while(~scanf("%d %d",&n,&m))    {        for(int i=0; i<n; i++)        {            scanf("%d",&Arr[i]);        }        if(m>n)        {            printf("ERROR\n");            continue;        }        Qsort(0,n-1);        for(int i=0; i<m; i++)        {            if(i)                printf(" ");            printf("%d",Arr[i]);        }        printf("\n");    }    return 0;}Sort:#include <bits/stdc++.h>#define RR freopen("input.txt","r",stdin)#define WW freopen("output.txt","w",stdout)#define ClearAll(A,T) memset(A,T,sizeof(A))using namespace std;const int Max=110000;int Arr[Max];bool cmp(int a,int b){    return a>b;}int main(){    int n,m;    while(~scanf("%d %d",&n,&m))    {        for(int i=0; i<n; i++)        {            scanf("%d",&Arr[i]);        }        if(m>n)        {            printf("ERROR\n");            continue;        }        sort(Arr,Arr+n,cmp);        for(int i=0; i<m; i++)        {            if(i)                printf(" ");            printf("%d",Arr[i]);        }        printf("\n");    }    return 0;}归并排序:#include <bits/stdc++.h>#define RR freopen("input.txt","r",stdin)#define WW freopen("output.txt","w",stdout)#define ClearAll(A,T) memset(A,T,sizeof(A))using namespace std;const int Max=110000;int Arr[Max];int Tarr[Max];void Merge(int low,int mid,int high){    int i=low,j=mid+1,k=0;    while(i<=mid&&j<=high)    {        if(Arr[i]>=Arr[j])            Tarr[k++]=Arr[i++];        else            Tarr[k++]=Arr[j++];    }    while(i<=mid)    {        Tarr[k++]=Arr[i++];    }    while(j<=high)    {        Tarr[k++]=Arr[j++];    }    for(i=0; i<k; i++)    {        Arr[low+i]=Tarr[i];    }}void Qsort(int low,int high){    if(low<high)    {        int mid=(low+high)/2;        Qsort(low,mid);        Qsort(mid+1,high);        Merge(low,mid,high);    }}int main(){    int n,m;    while(~scanf("%d %d",&n,&m))    {        for(int i=0; i<n; i++)        {            scanf("%d",&Arr[i]);        }        if(m>n)        {            printf("ERROR\n");            continue;        }        Qsort(0,n-1);        for(int i=0; i<m; i++)        {            if(i)                printf(" ");            printf("%d",Arr[i]);        }        printf("\n");    }    return 0;}/*先建堆后调整;*/#include <bits/stdc++.h>#define RR freopen("input.txt","r",stdin)#define WW freopen("output.txt","w",stdout)#define ClearAll(A,T) memset(A,T,sizeof(A))using namespace std;const int Max=110000;int Arr[Max];void MaxHeap(int i,int n){    int j,ans;    ans=Arr[i];    j=2*i+1;    while(j<n)    {        if(j+1<n&&Arr[j+1]<Arr[j])        {            j++;        }        if(Arr[j]>=ans)        {            break;        }        Arr[i]=Arr[j];        i=j;        j=2*i+1;    }    Arr[i]=ans;}void MakeMaxHeap(int n){    for(int i=n/2-1;i>=0;i--)    {        MaxHeap(i,n);    }}void Heap(int n){    for(int i=n-1;i>=0;i--)    {        swap(Arr[i],Arr[0]);        MaxHeap(0,i);    }}int main(){    int n,m;    while(~scanf("%d %d",&n,&m))    {        for(int i=0;i<n;i++)        {            scanf("%d",&Arr[i]);        }        if(m>n)        {            printf("ERROR\n");            continue;        }        MakeMaxHeap(n);        Heap(n);        for(int i=0;i<m;i++)        {            if(i)                printf(" ");            printf("%d",Arr[i]);        }        printf("\n");    }    return 0;}




0 0
原创粉丝点击