题目 1 相反数
时间限制: 1 秒
空间限制: 256 MB
问题描述
有 N 个非零且各不相同的整数。请你编一个程序求出它们中有多少对
相反数(a 和 −a 为一对相反数)。
输入格式
第一行包含一个正整数 N。(1 ≤ N ≤ 500)。
第二行为 N 个用单个空格隔开的非零整数,每个数的绝对值不超过
1 000,保证这些整数各不相同。
输出格式
只输出一个整数,即这 N 个数中包含多少对相反数。
思路:
因为数字绝对值不大觉得可以打表
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <map>
using namespace std;
int main(){
int N,num,i;
int ans = 0;
map<int,int> hash;
cin>>N;
for(i=-1000;i<=1000;i++){
hash.insert(pair<int,int>(i,0));
}
while(N){
cin>>num;
hash[num]++;
N--;
}
for(i=0;i<=1000;i++){
ans+=min(hash[i],hash[-i]);
}
cout<<ans<<endl;
system("pause");
return 0;
}