數據結構實驗之查找七:線性之哈希表

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,i,m;
    int has[1100];
    while(cin>>a>>b)
    {
        memset(has,0,sizeof(has));
            for(i=1;i<=a;i++)
            {
                    cin>>m;
                    if(has[m%b]==0||has[m%b]==m)
                    {
                        has[m%b]=m;
                        cout<<m%b;
                    }
                    else
                    {
                        int k=m%b;
                        while(has[k]!=0)
                        {
                            if(k==b-1)
                            {
                                k=0;
                            }
                            else
                            {
                                k++;
                            }
                            if(has[k]==m)
                            {
                                break;
                            }
                        }
                        has[k]=m;
                        cout<<k;
                    }
                if(i==a)
                {
                    cout<<endl;
                }
                else
                {
                    cout<<" ";
                }
            }

    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章