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

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

题目描述

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

输入

 多组输入,每组输入格式如下。

第一行包含两个整数n m。(n<=100000, m>0)
第二行包含n个正整数。

输出

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

示例输入

2 1
4 3
4 2
1 2 898989 23

示例输出

4
898989 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;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

排序练习——找出前m大的数字 分类: 排序 2015-06-08 09:33 21人阅读 评论(0) 收藏的更多相关文章

  1. Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏

    胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Subm ...

  2. 8大排序算法图文讲解 分类: Brush Mode 2014-08-18 11:49 78人阅读 评论(0) 收藏

    排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 常见的内部排序算法有:插入排序.希尔排序. ...

  3. Hdu1429 胜利大逃亡(续) 2017-01-20 18:33 53人阅读 评论(0) 收藏

    胜利大逃亡(续) Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Subm ...

  4. SQL string类型的数据按int类型排序 分类: SQL Server 2014-12-08 16:56 393人阅读 评论(0) 收藏

    说明: 我在做wms进销存软件时,发现一个问题:一张入库单(T_OutIn_BoxTop),入库扫描时要分成多箱,箱号(BoxTop_No)可以是数字也可以是字符串,所以箱号只能是字符串类型的,问题来 ...

  5. 合并k个已排序的链表 分类: leetcode 算法 2015-07-09 17:43 3人阅读 评论(0) 收藏

    最先想到的是把两个linked lists 合并成一个. 这样从第一个开始一个一个吞并,直到所有list都被合并. class ListNode:# Definition for singly-lin ...

  6. 8大排序算法图文讲解 分类: B10_计算机基础 2014-08-18 15:36 243人阅读 评论(0) 收藏

    排序算法可以分为内部排序和外部排序,内部排序是数据记录在内存中进行排序,而外部排序是因排序的数据很大,一次不能容纳全部的排序记录,在排序过程中需要访问外存. 常见的内部排序算法有:插入排序.希尔排序. ...

  7. 各种排序算法的分析及java实现 分类: B10_计算机基础 2015-02-03 20:09 186人阅读 评论(0) 收藏

    转载自:http://www.cnblogs.com/liuling/p/2013-7-24-01.html 另可参考:http://gengning938.blog.163.com/blog/sta ...

  8. C#期末大作业 消消乐 2017-06-01 18:11 275人阅读 评论(0) 收藏

    邻近期末,忙于刷题之余意识到期末大作业来不及了,匆匆赶下了作业,虽说做的很是粗糙,但完全原创的 下载链接 https://pan.baidu.com/s/1cCNLr4 大体的做大约3天完成了: 第一 ...

  9. HDU1253 胜利大逃亡(BFS) 2016-07-24 13:41 67人阅读 评论(0) 收藏

    胜利大逃亡 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示 ...

随机推荐

  1. Vue.js实例练习

    最近学习Vue.js感觉跟不上节奏了,Vue.js用起来很方便. 主要实现功能,能添加书的内容和删除.(用的Bootstrap的样式)demo链接 标题用了自定义组件,代码如下: components ...

  2. display:inline-block; 到底是个啥玩意?

    display:inline; 内联元素,简单来说就是在同一行显示.display:block; 块级元素,简单来说就是就是有换行,会换到第二行.display:inline-block; 就是在同一 ...

  3. Leetcode: Water and Jug Problem && Summary: GCD求法(辗转相除法 or Euclidean algorithm)

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  4. sdutoj 2154 Shopping

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2154 Shopping Time Limit: ...

  5. zjuoj 3600 Taxi Fare

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3600 Taxi Fare Time Limit: 2 Seconds    ...

  6. codevs 2235 机票打折

    http://codevs.cn/problem/2235/ 2235 机票打折  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 青铜 Bronze 题解       题目描述  ...

  7. C++之路进阶——bzoj1030(文本生成器)

    F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser  hyxzc Logout 捐赠本站 Notice:由于本OJ建立在 ...

  8. 变形--缩放 scale()

    缩放 scale()函数 让元素根据中心原点对对象进行缩放. 缩放 scale 具有三种情况: 1. scale(X,Y)使元素水平方向和垂直方向同时缩放(也就是X轴和Y轴同时缩放) 例如: div: ...

  9. 远程mysql服务器无法连接解决方案

    错误现象:Habon被拒绝. 远程服务器无法连接从两个方面看 1.是否能ping通远程服务器 windows下查看防火墙是否已关闭 linux下查看iptables等 2.数据库是否有开用户管理权限 ...

  10. Mysql错误处理

    有几种错误处理的声明形式: § 如果任何错误(不是 后继续执行: DECLARE CONTINUE HANDLER FOR SQLEXCEPTION ; § 如果发生任何错误(不是 NOT FOUND ...