当前位置:编程学习 > C#/ASP.NET >>

hdu 1280 前m大的数 (hash)

典型的hash: 用数组下标表示两两相加所得到的和,开辟一个满足题意的大小的数组 sum,

这样下标由大到小输出m个就可以 */


[csharp] 
#include <stdio.h> 
#include <string.h> 
int main () 

    int a[3001]; 
    int sum[10010]; 
    int n, m; 
    int i,j; 
    while ( scanf ("%d %d", &n, &m) != EOF ) 
    { 
        memset ( a, 0, sizeof (a) ); 
        memset ( sum, 0, sizeof (sum) ); 
        for ( i = 0; i < n; i ++ ) 
        { 
            scanf ("%d", &a[i]); 
        } 
         
        int temp; 
        for ( i = 0; i < n; i ++ ) 
        { 
            for ( j = i + 1; j < n; j++ ) 
            { 
                temp = a[i] + a[j]; 
                sum[temp] ++; 
            } 
        } 
         
        int count = 0;      //输出前 m  个数 
        for ( i = 10001; i >= 0 ; i -- ) 
        { 
            if ( sum[i] ) 
            { 
                for (j = 0; j < sum[i]; j ++) 
                { 
                    count ++; 
                    count == 1 ? printf ("%d", i) : printf (" %d", i); 
                    if ( count == m ) 
                        break; 
                } 
            } 
            if ( count == m ) 
                break; 
        } 
         
        printf ("\n"); 
    } 
    return 0; 

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,