zoukankan      html  css  js  c++  java
  • HDU1280 前m大的数

    前m大的数

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 3103    Accepted Submission(s): 1096


    Problem Description
    还记得Gardon给小希布置的那个作业么?(上次比赛的1005)其实小希已经找回了原来的那张数表,现在她想确认一下她的答案是否正确,但是整个的答案是很庞大的表,小希只想让你把答案中最大的M个数告诉她就可以了。 
    给定一个包含N(N<=3000)个正整数的序列,每个数不超过5000,对它们两两相加得到的N*(N-1)/2个和,求出其中前M大的数(M<=1000)并按从大到小的顺序排列。
     

    Input
    输入可能包含多组数据,其中每组数据包括两行: 
    第一行两个数N和M, 
    第二行N个数,表示该序列。

     

    Output
    对于输入的每组数据,输出M个数,表示结果。输出应当按照从大到小的顺序排列。
     

    Sample Input
    4 4 1 2 3 4 4 5 5 3 6 4
     

    Sample Output
    7 6 5 5 11 10 9 9 8
     

      一道简单的Hash题,通过组合生成各种组合数,再通过一个数组来标记是否存在该数。

    代码入下:

    

    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #include <time.h> 
    #include <conio.h>
    
    
    
    
    int hash[10005], ele[3005], add[3005];
    
    int main()
    {
        int N, M;
        while( scanf( "%d%d", &N, &M )!= EOF )
        {
            memset( hash, 0, sizeof( hash ) );
            for( int i= 0; i< N; ++i )
                scanf( "%d", &ele[i] );
            for( int i= 0; i< N; ++i )
                for( int j= i+ 1; j<N; ++j )
                    hash[ ele[i]+ ele[j] ]++;
            for( int i= 10000, k= 0; k< M; --i )// 可优化
            {
                while( hash[i]-- )
                {
                    add[k++]= i;
                }
            }
            for( int i= 0; i< M; ++i )
            {
                printf( "%d", add[i] );
                if( i< M- 1 )
                    printf( " " );
            }
            puts( "" );
        }
        return 0;
    }  
    


  • 相关阅读:
    Docker学习(一)
    mysql定时任务
    如何查看电脑是几核的?
    卸载Mysql connect 6.9.9
    找不到该项目怎么删除
    jmeter录制app脚本
    postman使用
    排查linux系统是否被入侵
    部署apache-tomcat环境
    sudo初级授权设置
  • 原文地址:https://www.cnblogs.com/Lyush/p/2058316.html
Copyright © 2011-2022 走看看