zoukankan      html  css  js  c++  java
  • A1078 Hashing [质数和散列结合]

    在这里插入图片描述

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<math.h>
    using namespace std;
    const int N = 10001;
    bool hashtable[N] = { 0 };
    bool isprime(int n) {
    	if (n <= 1) return false;
    	int sqr = sqrt(n);
    	for (int i = 2; i <= sqr; i++) {
    		if (n % i == 0)
    			return false;
    	}
    	return true;
    }
    int main()
    {
    	int n, Tsize, a;
    	cin >> Tsize >> n;
    	while (isprime(Tsize) == false)
    		Tsize++;
    	for (int i = 0; i < n; i++)
    	{
    		cin >> a;
    		int M = a % Tsize;
    		if (hashtable[M] == false)
    		{
    			hashtable[M] = true;
    			if (i == 0) cout << M;
    			else cout << " " << M;
    		}
    		else
    		{
    			int step;
    			for (step = 1; step < Tsize; step++)
    			{
    				M = (a + step * step) % Tsize;
    				if (hashtable[M] == false)
    				{
    					hashtable[M] = true;
    					if (i == 0) cout << M;
    					else cout << " " << M;
    					break;
    				}
    			}
    			if (step == Tsize)
    			{
    				if (i > 0)
    					cout << " " << "-";
    			}
    		}
    	}
    
    }
    
  • 相关阅读:
    Set,List,Map的区别
    阅读笔记15
    阅读笔记14
    阅读笔记13
    阅读笔记12
    阅读笔记11
    阅读笔记10
    架构漫谈读后感
    阅读笔记1
    暑期周记8
  • 原文地址:https://www.cnblogs.com/Hsiung123/p/13812043.html
Copyright © 2011-2022 走看看