PAT 1078 Hashing

來源:互聯網
上載者:User

標籤:blog   io   os   sp   for   2014   log   amp   as   

# include <stdio.h># include <algorithm># include <string.h># include <iostream># include <math.h>using namespace std;int prime[10100];int vis[10110];int cot;void init_prime(){    memset(vis,0,sizeof(vis));    cot=0;    for(int i=2; i<=10100; i++)    {        if(!vis[i])        {            prime[cot++]=i;            for(int j=i*2; j<=10100; j+=i)                vis[j]=1;        }    }}int main(){    int n,size1,i,a,ans,j;    int map[10100];    while(~scanf("%d%d",&size1,&n))    {        init_prime();        memset(map,0,sizeof(map));        for(i=0; i<cot; i++)        {            if(prime[i]>=size1)            {                size1=prime[i];                break;            }        }        for(i=0; i<n; i++)        {            scanf("%d",&a);            ans=a%size1;            if(!map[ans])            {                if(i==0)                    printf("%d",ans);                else                    printf(" %d",ans);                map[ans]=1;            }            else/*二次探測法的公式: hi=(h(key)+i*i)%size1 1≤i≤size1-1 //即di=i2                               即探查序列為d=h(key),d+1^2,d+2^2,…d+(size1-1)^2*/            {                for(j=1; j<size1; j++)                {                    int kk=(ans+j*j)%size1;                    if(!map[kk])                    {                        map[kk]=1;                        if(i==0)                            printf("%d",kk);                        else                            printf(" %d",kk);                        break;                    }                }                if(j==size1)                {                    if(i==0)                        printf("-");                    else                        printf(" -");                }            }        }        printf("\n");    }    return 0;}

PAT 1078 Hashing

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.