time limit: 1000 ms
memory limit: 65536 kib
problem description
根據給定的一系列整數關鍵字和素數p,用除留餘數法定義hash函式h(key)=key%p,將關鍵字對映到長度為p的雜湊表中,用線性探測法解決衝突。重複關鍵字放在hash表中的同一位置。
input
連續輸入多組資料,每組輸入資料第一行為兩個正整數n(n <= 1500)和p(p >= n的最小素數),n是關鍵字總數,p是hash表長度,第2行給出n個正整數關鍵字,數字間以空格間隔。
output
輸出每個關鍵字在hash表中的位置,以空格間隔。注意最後乙個數字後面不要有空格。
sample input
5 521 21 21 21 21
4 524 15 61 88
4 524 39 61 15
5 524 39 61 15 39
sample output
1 1 1 1 14 0 1 3
4 0 1 2
4 0 1 2 0
hint
source
//根據題意構造探測函式 然後迴圈探測
#include #include #include #include #include using namespace std;
int dis[1600];
int hash[1600];///主要是檢測該點是否出現過
int locate[1600];///儲存
int main()
while(~scanf("%d%d",&n,&m))
else if(hash[k] == key) ///已經儲存了該數字,直接記錄位置
///二次探測再雜湊}}
int flag = 1;
for(int i = 0; i < n; i++)
else
printf(" %d",locate[i]);
}printf("\n");
}return 0;
}
資料結構實驗之查詢七 線性之雜湊表
time limit 1000ms memory limit 65536k 有疑問?點這裡 根據給定的一系列整數關鍵字和素數p,用除留餘數法定義hash函式h key key p,將關鍵字對映到長度為p的雜湊表中,用線性探測法解決衝突。重複關鍵字放在hash表中的同一位置。連續輸入多組資料,每組輸入...
資料結構實驗之查詢七 線性之雜湊表
根據給定的一系列整數關鍵字和素數p,用除留餘數法定義hash函式h key key p,將關鍵字對映到長度為p的雜湊表中,用線性探測法解決衝突。重複關鍵字放在hash表中的同一位置。連續輸入多組資料,每組輸入資料第一行為兩個正整數n n 1000 和p p n的最小素數 n是關鍵字總數,p是hash...
資料結構實驗之查詢七 線性之雜湊表
time limit 1000ms memory limit 65536k 有疑問?點這裡 根據給定的一系列整數關鍵字和素數p,用除留餘數法定義hash函式h key key p,將關鍵字對映到長度為p的雜湊表中,用線性探測法解決衝突。重複關鍵字放在hash表中的同一位置。連續輸入多組資料,每組輸入...