4318: 【基础】统计数对个数(1766)

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:30 解决:17

题目描述

考虑一组 lns="http://www.w3.org/1998/Math/MathML"> 个不同的正整数 lns="http://www.w3.org/1998/Math/MathML">1,2,..., ,它们的值在 lns="http://www.w3.org/1998/Math/MathML">1 到 lns="http://www.w3.org/1998/Math/MathML">1000000 之间。给定一个整数 lns="http://www.w3.org/1998/Math/MathML">

写一个程序计算这样的数对个数 lns="http://www.w3.org/1998/Math/MathML">(,) ,lns="http://www.w3.org/1998/Math/MathML">1< 并且 lns="http://www.w3.org/1998/Math/MathML">+= 。

输入

标准输入的第一行是一个整数 lns="http://www.w3.org/1998/Math/MathML"> ( lns="http://www.w3.org/1998/Math/MathML">11000000 )。

第二行有 lns="http://www.w3.org/1998/Math/MathML"> 个整数表示元素。

第三行是一个整数 lns="http://www.w3.org/1998/Math/MathML"> ( lns="http://www.w3.org/1998/Math/MathML">12000000 )。

输出

输出一行包含一个整数表示这样的数对个数。

样例输入 复制

9
5 12 7 10 9 1 2 3 11
13

样例输出 复制

3

提示

不同的和为 lns="http://www.w3.org/1998/Math/MathML">13 的数对是 lns="http://www.w3.org/1998/Math/MathML">(12,1)lns="http://www.w3.org/1998/Math/MathML">(10,3) 和 lns="http://www.w3.org/1998/Math/MathML">(2,11) 。

#include<bits/stdc++.h>
using namespace std;
set<int> s;
int n,i,t,x,ans;
int main(){
	cin>>n;
	for (i=0;i<n;i++) {
		cin>>t;
		s.insert(t);
	}
	cin>>x;
	set<int>::iterator it;
	for(it=s.begin();it!=s.end();it++) {
		if (s.find(x-*it)!=s.end())
		    ans++;
	}
	ans/=2;
	cout<<ans;
	return 0;
}