201403-1

map

 
问题描述
  有 N 个非零且各不相同的整数。请你编一个程序求出它们中有多少对相反数(a 和 -a 为一对相反数)。
输入格式
  第一行包含一个正整数 N。(1 ≤ N ≤ 500)。
  第二行为 N 个用单个空格隔开的非零整数,每个数的绝对值不超过1000,保证这些整数各不相同。
输出格式
  只输出一个整数,即这 N 个数中包含多少对相反数。
样例输入
5
1 2 3 -1 -2
样例输出
 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<vector>
 4 #include<map>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     map<int,int>m;   //    map 的初始值,,内存分配 
10     
11 //    cout<<m[3]<<endl;
12     int n;
13     cin>>n;
14     
15     int res = 0;
16     
17     for (int i=0;i<n;i++)
18     {
19         int t;
20         cin>>t;
21         
22         m[t] = 1;
23         
24         int xt = -t;
25         
26         if(m[xt]==1)res++;
27         
28     }
29     cout<<res;
30     return 0;
31  } 
原文地址:https://www.cnblogs.com/wuxiaotianC/p/9503958.html