1078 Hashing (25分)测试点1错误,处理素数总是从2开始,把1忘了

isprime没有判断1,走程序1是素数。1不是素数,要特判返回false

bool isprime(int a)
{
    
    
    if(a==1) return false;
    int sqr=sqrt(a*1.0);
    for(int i=2;i<=sqr;i++)
    {
    
    
        if(a%i==0)
            return false;
    }
    return true;
}
#include<bits/stdc++.h>
using namespace std;
bool isprime(int a)
{
    
    
    int sqr=sqrt(a*1.0);
    for(int i=2;i<=sqr;i++)
    {
    
    
        if(a%i==0)
            return false;
    }
    return true;
}
int main()
{
    
    
    bool hashtable[10010]={
    
    false};
    int n,m,temp;
    cin>>n>>m;
    while(!isprime(n))
        n++;
    cin>>temp;
     hashtable[temp%n]=true;
     cout<<temp%n;
    for(int i=1;i<m;i++)
    {
    
    
        int step=0;
        cin>>temp;
        while(hashtable[(temp+step*step)%n])
        {
    
    
            step++;
            if(step==n+1)
            {
    
    
                cout<<" -";
                break;
            }
        }
        if(step<=n)
        {
    
    
            hashtable[(temp+step*step)%n]=true;
            cout<<" "<<(temp+step*step)%n;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42835526/article/details/113505488
今日推荐