hdu1280前m大的数

来源:互联网 发布:手机屏幕变魔术的软件 编辑:程序博客网 时间:2024/05/21 19:27
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1280

简单的hash。。。


代码:


#include <cstdio>#include <cstring>int hash[10005];int sum[10005];int a[3005];int n,m;int main(){    while(~scanf("%d%d",&n,&m))    {        memset(sum,0,sizeof sum);        memset(hash,0,sizeof hash);        for(int i = 0;i < n;++i)            scanf("%d",&a[i]);        for(int i = 0;i < n;++i)        {            for(int j = i + 1;j < n;++j)            {                sum[a[i] + a[j]] ++;                hash[a[i] + a[j]] = 1;            }        }        int cnt = 1;        for(int i = 10000;i >= 0 && cnt <= m;--i)        {            if(hash[i])            {                while(sum[i]-- && cnt <= m)                {                    if(cnt == 1)                        printf("%d",i);                    else                        printf(" %d",i);                    ++cnt;                }            }        }        printf("\n");    }}

0 0
原创粉丝点击