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

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

题目描述

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

输入

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

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

输出

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

示例输入

  1. 2 1
  2. 4 3
  3. 4 2
  4. 1 2 898989 23

示例输出

  1. 4
  2. 898989 23
  3. 基数排序
  4. #include <cstdio>
  5. #include <cmath>
  6. #include <cstring>
  7. #include <cstdlib>
  8. #include <iostream>
  9. using namespace std;
  10. const int MAX=110000;
  11. int Arr[MAX];
  12. int Getdigit(int x,int d)
  13. {
  14.     return (x/d)%10;//计算每位的数字
  15. }
  16. void MSD_sort(int low,int high,int d)
  17. {
  18.     int count[10]= {0};
  19.     int *b=new int[high-low+1];//为所有桶开辟空间
  20.     int sum=1;
  21.     for(int k=1;k<=d;k++)
  22.     {
  23.         memset(count,0,sizeof(count));
  24.         for(int i=low;i<=high;i++)
  25.         {
  26.             count[Getdigit(Arr[i],sum)]++;//统计数据的数量
  27.         }
  28.         for(int i=1;i<=9;i++)
  29.         {
  30.             count[i]+=count[i-1];//设置每个桶的右边界索引
  31.         }
  32.         //讲数据装入桶中
  33.         for(int i=high;i>=low;i--)//从右向左,保证数据的稳定性
  34.         {
  35.             int ans=Getdigit(Arr[i],sum);//计算关键码的数字;
  36.             b[count[ans]-1]=Arr[i];//放入对应的桶中,aount[ans]-1为右边界索引
  37.             --count[ans];
  38.         }
  39.         //收集数据
  40.         for(int i=low,j=0;i<=high;i++,j++)
  41.         {
  42.             Arr[i]=b[j];
  43.         }
  44.         sum*=10;
  45.     }
  46.     delete b;//删除
  47. }
  48. int main()
  49. {
  50.     int n,m;
  51.     while(~scanf("%d %d",&n,&m ))
  52.     {
  53.         for(int i=0; i<n; i++)
  54.         {
  55.             scanf("%d",&Arr[i]);
  56.         }
  57.         if(m>n)
  58.         {
  59.             cout<<"ERROR"<<endl;
  60.             continue;
  61.         }
  62.         MSD_sort(0,n-1,9);
  63.         for(int i=n-1; i>=(n-m); i--)
  64.         {
  65.             if(i!=n-1)
  66.                 printf(" ");
  67.             printf("%d",Arr[i]);
  68.         }
  69.         printf("\n");
  70.     }
  71.     return 0;
  72. }
  73. <pre name="code" class="cpp">#include <bits/stdc++.h>
  74. #define RR freopen("input.txt","r",stdin)
  75. #define WW freopen("output.txt","w",stdout)
  76. #define ClearAll(A,T) memset(A,T,sizeof(A))
  77. /*
  78. 手敲快排
  79. */
  80. using namespace std;
  81. const int Max=110000;
  82. int Arr[Max];
  83. void Qsort(int low,int high)
  84. {
  85.     int i=low,j=high,s=Arr[low];
  86.     if(i>=j)
  87.         return ;
  88.     while(i<j)
  89.     {
  90.         while(i<j&&Arr[j]<=s)
  91.             j--;
  92.         Arr[i]=Arr[j];
  93.         while(i<j&&Arr[i]>=s)
  94.             i++;
  95.         Arr[j]=Arr[i];
  96.     }
  97.     Arr[i]=s;
  98.     Qsort(low,i-1);
  99.     Qsort(i+1,high);
  100. }
  101. int main()
  102. {
  103.     int n,m;
  104.     while(~scanf("%d %d",&n,&m))
  105.     {
  106.         for(int i=0; i<n; i++)
  107.         {
  108.             scanf("%d",&Arr[i]);
  109.         }
  110.         if(m>n)
  111.         {
  112.             printf("ERROR\n");
  113.             continue;
  114.         }
  115.         Qsort(0,n-1);
  116.         for(int i=0; i<m; i++)
  117.         {
  118.             if(i)
  119.                 printf(" ");
  120.             printf("%d",Arr[i]);
  121.         }
  122.         printf("\n");
  123.     }
  124.     return 0;
  125. }
  126. Sort:
  127. #include <bits/stdc++.h>
  128. #define RR freopen("input.txt","r",stdin)
  129. #define WW freopen("output.txt","w",stdout)
  130. #define ClearAll(A,T) memset(A,T,sizeof(A))
  131. using namespace std;
  132. const int Max=110000;
  133. int Arr[Max];
  134. bool cmp(int a,int b)
  135. {
  136.     return a>b;
  137. }
  138. int main()
  139. {
  140.     int n,m;
  141.     while(~scanf("%d %d",&n,&m))
  142.     {
  143.         for(int i=0; i<n; i++)
  144.         {
  145.             scanf("%d",&Arr[i]);
  146.         }
  147.         if(m>n)
  148.         {
  149.             printf("ERROR\n");
  150.             continue;
  151.         }
  152.         sort(Arr,Arr+n,cmp);
  153.         for(int i=0; i<m; i++)
  154.         {
  155.             if(i)
  156.                 printf(" ");
  157.             printf("%d",Arr[i]);
  158.         }
  159.         printf("\n");
  160.     }
  161.     return 0;
  162. }
  163. 归并排序:
  164. #include <bits/stdc++.h>
  165. #define RR freopen("input.txt","r",stdin)
  166. #define WW freopen("output.txt","w",stdout)
  167. #define ClearAll(A,T) memset(A,T,sizeof(A))
  168. using namespace std;
  169. const int Max=110000;
  170. int Arr[Max];
  171. int Tarr[Max];
  172. void Merge(int low,int mid,int high)
  173. {
  174.     int i=low,j=mid+1,k=0;
  175.     while(i<=mid&&j<=high)
  176.     {
  177.         if(Arr[i]>=Arr[j])
  178.             Tarr[k++]=Arr[i++];
  179.         else
  180.             Tarr[k++]=Arr[j++];
  181.     }
  182.     while(i<=mid)
  183.     {
  184.         Tarr[k++]=Arr[i++];
  185.     }
  186.     while(j<=high)
  187.     {
  188.         Tarr[k++]=Arr[j++];
  189.     }
  190.     for(i=0; i<k; i++)
  191.     {
  192.         Arr[low+i]=Tarr[i];
  193.     }
  194. }
  195. void Qsort(int low,int high)
  196. {
  197.     if(low<high)
  198.     {
  199.         int mid=(low+high)/2;
  200.         Qsort(low,mid);
  201.         Qsort(mid+1,high);
  202.         Merge(low,mid,high);
  203.     }
  204. }
  205. int main()
  206. {
  207.     int n,m;
  208.     while(~scanf("%d %d",&n,&m))
  209.     {
  210.         for(int i=0; i<n; i++)
  211.         {
  212.             scanf("%d",&Arr[i]);
  213.         }
  214.         if(m>n)
  215.         {
  216.             printf("ERROR\n");
  217.             continue;
  218.         }
  219.         Qsort(0,n-1);
  220.         for(int i=0; i<m; i++)
  221.         {
  222.             if(i)
  223.                 printf(" ");
  224.             printf("%d",Arr[i]);
  225.         }
  226.         printf("\n");
  227.     }
  228.     return 0;
  229. }
  230. /*
  231. 先建堆后调整;
  232. */
  233. #include <bits/stdc++.h>
  234. #define RR freopen("input.txt","r",stdin)
  235. #define WW freopen("output.txt","w",stdout)
  236. #define ClearAll(A,T) memset(A,T,sizeof(A))
  237. using namespace std;
  238. const int Max=110000;
  239. int Arr[Max];
  240. void MaxHeap(int i,int n)
  241. {
  242.     int j,ans;
  243.     ans=Arr[i];
  244.     j=2*i+1;
  245.     while(j<n)
  246.     {
  247.         if(j+1<n&&Arr[j+1]<Arr[j])
  248.         {
  249.             j++;
  250.         }
  251.         if(Arr[j]>=ans)
  252.         {
  253.             break;
  254.         }
  255.         Arr[i]=Arr[j];
  256.         i=j;
  257.         j=2*i+1;
  258.     }
  259.     Arr[i]=ans;
  260. }
  261. void MakeMaxHeap(int n)
  262. {
  263.     for(int i=n/2-1;i>=0;i--)
  264.     {
  265.         MaxHeap(i,n);
  266.     }
  267. }
  268. void Heap(int n)
  269. {
  270.     for(int i=n-1;i>=0;i--)
  271.     {
  272.         swap(Arr[i],Arr[0]);
  273.         MaxHeap(0,i);
  274.     }
  275. }
  276. int main()
  277. {
  278.     int n,m;
  279.     while(~scanf("%d %d",&n,&m))
  280.     {
  281.         for(int i=0;i<n;i++)
  282.         {
  283.             scanf("%d",&Arr[i]);
  284.         }
  285.         if(m>n)
  286.         {
  287.             printf("ERROR\n");
  288.             continue;
  289.         }
  290.         MakeMaxHeap(n);
  291.         Heap(n);
  292.         for(int i=0;i<m;i++)
  293.         {
  294.             if(i)
  295.                 printf(" ");
  296.             printf("%d",Arr[i]);
  297.         }
  298.         printf("\n");
  299.     }
  300.     return 0;
  301. }
  302.  
  1.  

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

排序练习——找出前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. redhat 6.7 安装nvidia显卡驱动时出现的问题

    一.给Redhat装Nvidia驱动时,出现类似ERROR: The Nouveau kernel driver is currently in use by your system. 的错误,这是应 ...

  2. 数组 splice 方法

    splice 是万能的吗? 不知道,至少他有三种功能 splice的三种功能,减去,增加,替换 第一种减去 var s=[1,2,3,4,5,6]; s.splice(2,2); console.lo ...

  3. lcd 图片

    硬件平台:mini2440 软件环境:UCOS2 .ADS1.2 . LCD彩色图片转换工具BMP_to_H工具bmp2h LCD彩色图片转换工具BMP_to_H工具文件夹下的使用说明 在S3C241 ...

  4. ServiceController1

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. 20145207 《Java程序设计》第10周学习总结

    前言:   最后一篇java博客好激动啊..不过猜猜我在干什么?没错,安虚拟机,唉!紧接着又是一大波信安系统的博客,真开心~好啦边敲博客,边装虚拟机. 教材知识汇总 13.1 网络概述 13.1.1计 ...

  6. 常见的appbug(转)

    移动App Bug的影响是用户体验差.App的商店评级下降.用户换用竞争对手的App,声誉和信誉损失.最后销售量减少,如果它是一个付费App的话. 移动App测试与传统台式机测试相比有一定的复杂性.这 ...

  7. nginx的基本配置和虚拟主机的配置

    在Nginx配置文件(nginx.conf)中,一个最简化的虚拟主机配置代码如下: 跟Apache -样,Nginx也可以配置多种类型的虚拟圭机:一是基于IP的虚拟主机,二是基于域名的虚拟主机,三是基 ...

  8. 数据库中is null(is not null)与=null(!=null)的区别

    在标准SQL语言(ANIS SQL)SQL-92规定的Null值的比较取值结果都为False,既Null=Null取值也是False.NULL在这里是一种未知值,千变万化的变量,不是“空”这一个定值! ...

  9. POJ 3243 Clever Y(离散对数-拓展小步大步算法)

    Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...

  10. AMBA interconnector PL301(一)

    HPM(High-Performance Matrix)是一个自生成的AMBA3 bus subsystem. 由一个AXI bus matrix,Frequency Conversion Compo ...