为啥要绝对值??
查看原帖
为啥要绝对值??
229557
钊。源代码楼主2024/11/24 23:39

我的AC代码是这样子的

#include<cstdio>
using namespace std;
#define JQ 1000003
#define hash(a) a % JQ

long long n, C, ans;
int a[JQ+8];

struct node
{
    long long what_exactly_the_number_is;
    int how_many_times_this_number_appeared;
} hx[JQ];

long long abs_1(long long x)
{
    return x < 0 ? -x : x;
} //绝对值

int find(long long x)
{
    int y = hash(abs_1(x));
    while (hx[y].what_exactly_the_number_is != 0 && hx[y].what_exactly_the_number_is != x)
    {
        y++;
        y = hash(y);
    }
    return y; // y可以表示x被存在了哈希表的那个位置
}

void store(long long x)
{
    hx[find(x)].how_many_times_this_number_appeared++;
    if (hx[(find(x))].how_many_times_this_number_appeared == 1)
    {
        hx[find(x)].what_exactly_the_number_is = x;
    }
}


int main()
{
    ans = 0;
    scanf("%lld%lld", &n, &C);
    for(long long i=1;i<=n;i++){
        scanf("%d",&a[i]);
        store(a[i]);
    }
    for(long long i=1;i<=n;i++){
        ans += hx[find((a[i]-C))].how_many_times_this_number_appeared;
    }
    printf("%lld", ans); //输出
}

但是,我不明白,在find函数里面,为啥需要使用一次绝对值呢?(我没使用绝对值的时候,有两个测试点RE了,也不是WA,但是使用了这个绝对值之后,就通过了。)

这个是我审题的原因还是跟哈希表有关的知识?

2024/11/24 23:39
加载中...