201403-1 相反数

实现

#include <cstdio>

#define MAXN 0xfff

struct abs_str{
    int pos_cnt;
    int neg_cnt;
};

abs_str abs_sta[MAXN];

int main() {
    int num;
    scanf("%d",&num);
    
    int max_num=0;
    for (int i = 0;i < num;++i) {
        int figure;
        scanf("%d",&figure);
        if (figure<0) {
            abs_sta[-figure].neg_cnt++;
            max_num = (-figure > max_num)?-figure:max_num;
        } else {
            abs_sta[figure].pos_cnt++;
            max_num = (figure > max_num)?figure:max_num;
        }
    }

    int cnt = (abs_sta[0].pos_cnt * (abs_sta[0].pos_cnt - 1)) / 2;
    
    for (int i = 1;i <= max_num;++i) {
        if (abs_sta[i].neg_cnt != 0 && abs_sta[i].pos_cnt != 0) {
            cnt+= (abs_sta[i].neg_cnt + abs_sta[i].pos_cnt)/2;
        }
    }
    printf("%d",cnt);

}
posted @ 2020-08-23 11:30  amonqsq  阅读(60)  评论(0编辑  收藏  举报