其他分享
首页 > 其他分享> > PAT-1078 Hashing

PAT-1078 Hashing

作者:互联网

1078 Hashing

part 3, 3.1

自己解法

#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>

bool isPrime(int m)
{
    if (m <= 1)
        return false;
    for (int i = 2; i < m; i++)
        if (m % i == 0)
            return false;
    return true;
}

int main()
{
    int MS, N;
    cin >> MS >> N;
    int a, x;
    if (isPrime(MS) == false)
        for (; isPrime(MS) == false; MS++)
            ;
    vector<int> s(MS, -1);
    for (int i = 0; i < N; i++)
    {
        cin >> a;
        x = a % MS;
        if (s[x] < 0)
        {
            s[x] = a;
            cout << x;
        }
        else
        {
            int flag = 0;
            for (int j = 1; j < MS - 1; j++)
                if (s[(a + j * j) % MS] < 0)
                {
                    s[(a + j * j) % MS] = a;
                    cout << (a + j * j) % MS;
                    flag = 1;
                    break;
                }
            if (flag == 0)
                cout << "-";
        }
        if (i != N - 1)
            cout << " ";
    }
    cout << endl;
    system("pause");
    return 0;
}

大神解法

#include <iostream>
using namespace std;
int size, n, hashTable[10100];
bool isprime(int num) {
    if(num == 1) return false;
    for(int i = 2; i * i <= num; i++)
        if(num % i == 0) return false;
    return true;
}
void insert(int key) {
    for(int step = 0; step < size; step++) {
        int index = (key + step * step) % size;
        if(hashTable[index] == 0) {
            hashTable[index] = 1;
            cout << index % size;
            return ;
        }
    }
    cout << '-';
}
int main() {
    cin >> size >> n;
    while(!isprime(size)) size++;
    for(int i = 0; i < n; i++) {
        int key;
        cin >> key;
        if(i != 0) cout << ' ';
        insert(key);
    }
    return 0;
}

标签:include,PAT,1078,int,++,MS,false,Hashing,size
来源: https://blog.csdn.net/seven_colors/article/details/123139546