xtuoj1324 hash

Source Code

Problem: 1324		User: 202205******
Memory: 1196K		Time: 46MS
Language: GCC		Result: Accepted
Source Code

#include 
int hash(int a[], int b, int len)
{
    for (int i = 0; i < len; i++)
    {
        a[i] %= b;
    }
}
int copy(int b[], int a[], int len)
{
    for (int i = 0; i < len; i++)
    {
        b[i] = a[i];
    }
}
int hashtest(int a[105], int b)
{
    for (int i = 0; i < b - 1; i++)
    {
        for (int j = i + 1; j < b; j++)
        {
            if (a[i] == a[j])
            {
                return 0;
            }
        }
    }
    return 1;
}
int main()
{
    int n;
    scanf("%d", &n);
    while (n--)
    {
        int num;
        int input[110] = {0};
        int output[110] = {0};
        scanf("%d", &num);
        for (int i = 0; i < num; i++)
        {
            scanf("%d", &input[i]);
        }
        int put = -1;
        for (int i = 1; i <= 100000; i++)
        {
            copy(output, input,num);
            hash(output, i, num);
            if (hashtest(output, num))
            {
                put = i;
                break;
            }
        }
        printf("%d\n", put);
    }
}

你可能感兴趣的:(哈希算法,算法)