HDU-1280(哈希表求法)

题目名称:前m大的数  
题目链接:https://vjudge.net/problem/HDU-1280
#include 
#include 
#include 
#include 
#define getHassVal(i, j) arr[i] + arr[j]

using namespace std;
const int MAXN = 1500000;
int arr[5000];
int hashMap[MAXN];

int main()
{
    int n, m;
    while(scanf("%d%d", &n, &m) != EOF)
    {
        memset(hashMap, 0, sizeof(hashMap));
        for(int i = 0; i < n; ++i) cin >> arr[i];
        for(int i = 0; i < n; ++i)
            for(int j = i + 1; j < n; ++j) hashMap[getHassVal(i, j)]++;

        int flag = 0;
        for(int i = MAXN - 1; i > 0&&m>0; )
        {
            if(hashMap[i] == 0){i--; continue;}
            if(flag) cout << ' ' << i;
            else cout << i;
            flag = 1;
            hashMap[i]--;
            m--;
        }
        cout << endl;
    }
}

你可能感兴趣的:(哈希表)