美文网首页
1145 Hashing - Average Search Ti

1145 Hashing - Average Search Ti

作者: zjh3029 | 来源:发表于2018-08-05 17:06 被阅读0次
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <iomanip>
    using namespace std;
    
    bool isprime(int inp)
    {
        if (inp <= 0) return false;
        for (int i = 2; i <=inp/2; i++)
        {
            if (inp%i==0)
            {
                return false;
            }
        }
        return true;
    }
    
    int main()
    {
        int Msize, N, M;
        cin >> Msize >> N >> M;
        while(isprime(Msize) == false) Msize++;
    
        //cout << Msize << endl;
        vector<int> v(Msize);
        fill(v.begin(), v.end(), -1);
        int a,b;
        bool flag = false;
        for (int i = 0; i < N; i++)
        {
            cin >> a;
            for (int j = 0; j < Msize; j++)
            {
                b=(a + j * j) % Msize;
                if (v[b] == -1)
                {
                    v[b] = a;
                    flag = true;
                    break;
                }
            }
            if (flag==false)
            {
                cout << a << " cannot be inserted." << endl;
            }
            flag = false;
        }
    
        double ans = 0.0;
        for (int i = 0; i < M; i++)
        {
            cin >> a;
            int cnt = 1;
            for (int j = 0; j < Msize; j++)
            {
                b = (a + j * j) % Msize;
                if (v[b] == a||v[b]==-1) break;
                cnt++;
            }
            ans += cnt;
        }
        printf("%.1lf\n", ans / M);
        system("pause");
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:1145 Hashing - Average Search Ti

          本文链接:https://www.haomeiwen.com/subject/wjwbvftx.html