ccf 相反数

问题描述
问题描述
试题编号: 201403-1
试题名称: 相反数
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
问题描述
  有 N 个非零且各不相同的整数。请你编一个程序求出它们中有多少对相反数(a 和 -a 为一对相反数)。
输入格式
  第一行包含一个正整数 N。(1 ≤ N ≤ 500)。
  第二行为 N 个用单个空格隔开的非零整数,每个数的绝对值不超过1000,保证这些整数各不相同。
输出格式
  只输出一个整数,即这 N 个数中包含多少对相反数。
样例输入
5
1 2 3 -1 -2
样例输出
2
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 using namespace std;
 7 const int maxn=1007;
 8 int num[maxn];
 9 int main()
10 {
11    // freopen("in.txt","r",stdin);
12     int n,a;int ans=0;
13     scanf("%d",&n);
14     memset(num,0,sizeof(num));
15     for(int i=1;i<=n;i++)
16     {
17         scanf("%d",&a);
18         num[a+500]++;
19     }
20     for(int i=1;i<=500;i++)
21     {
22         ans+=min(num[i+500],num[500-i]);
23     }
24     printf("%d
",ans);
25     return 0;
26 }

原文地址:https://www.cnblogs.com/codeyuan/p/4374619.html