zoukankan      html  css  js  c++  java
  • 2Y

    给你n个整数,请按从大到小的顺序输出其中前m大的数。 

    Input

    每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。 

    Output

    对每组测试数据按从大到小的顺序输出前m大的数。 

    Sample Input

    5 3
    3 -35 92 213 -644

    Sample Output

    213 92 3 

    Hint

    请用VC/VC++提交


    // 不能用选择排序Time Limit Exceeded,最后用了希尔排序
     1 #include<stdio.h>
     2 int n, m, a[1000000];
     3 
     4 void Sort(int a[], int len)
     5 {
     6     int i, j, k, gap, temp;
     7     for(gap=len/2; gap>0; gap/=2)
     8     {
     9         for(i=gap; i<len; i++)
    10         {
    11             temp=a[i];
    12             for(j=i-gap; j>=0&&temp>a[j]; j-=gap)
    13             {
    14                 a[j+gap]=a[j];
    15             }
    16             a[j+gap]=temp;
    17         }
    18     }
    19 }
    20 
    21 int main()
    22 {
    23     int i;
    24     while(scanf("%d %d", &n, &m)!=EOF)
    25     {
    26         for(i=0;i<n;i++)
    27             scanf("%d", &a[i]);
    28         Sort(a, n);
    29         for(i=0;i<m;i++)
    30         {
    31             if(i==m-1) printf("%d
    ", a[i]);
    32             else printf("%d ", a[i]);
    33         }
    34     }
    35     return 0;
    36 }
    AC
    // 关于希尔排序的理解参考:
    https://blog.csdn.net/weixin_37818081/article/details/79202115
    https://www.cnblogs.com/daohuoren/p/6614766.html
    // 附上代码
     1 // 默认从小到大排序
     2 void selc_sort(int a[], int len);
     3 void shell_sort(int a[], int len);
     4 
     5 void selc_sort(int a[], int len)
     6 {
     7     int i, j, t, min;
     8     for(i=0;i<len-1;i++)
     9     {
    10         min=i;
    11         for(j=i+1;j<len;j++)
    12             if(a[min]>a[j]) min=j;
    13         if(min!=i)
    14         { t=a[i]; a[i]=a[min]; a[min]=t; }
    15     }
    16 }
    17 
    18 void shell_sort(int a[], int len)
    19 {
    20     int i, j, gap, t;
    21     for(gap=len/2; gap>0; gap/=2)
    22         for(i=gap; i<len; i++)
    23             for(j=i;j-gap>=0&&a[j]<a[j-gap];j-=gap)
    24             { t=a[j-gap]; a[j-gap]=a[j]; a[j]=t; }
    25 }
    sort.h
  • 相关阅读:
    证书格式转换
    emq知识点
    emq共享订阅
    SpringBoot
    Android网络编程Socket长连接
    Android 网络通信框架Volley简介(Google IO 2013)
    Android中的5种数据存储方式
    Android
    android解析XML总结(SAX、Pull、Dom三种方式)
    乔迁新禧
  • 原文地址:https://www.cnblogs.com/goldenretriever/p/10356702.html
Copyright © 2011-2022 走看看