Dating with girls(1)
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4875 Accepted Submission(s): 1560
Problem Description
Everyone in the HDU knows that the number of boys is larger than the number of girls. But now, every boy wants to date with pretty girls. The girls like to date with the boys with higher IQ. In order to test the boys ' IQ, The girls make a problem, and the
boys who can solve the problem
correctly and cost less time can date with them.
The problem is that : give you n positive integers and an integer k. You need to calculate how many different solutions the equation x + y = k has . x and y must be among the given n integers. Two solutions are different if x0 != x1 or y0 != y1.
Now smart Acmers, solving the problem as soon as possible. So you can dating with pretty girls. How wonderful!

correctly and cost less time can date with them.
The problem is that : give you n positive integers and an integer k. You need to calculate how many different solutions the equation x + y = k has . x and y must be among the given n integers. Two solutions are different if x0 != x1 or y0 != y1.
Now smart Acmers, solving the problem as soon as possible. So you can dating with pretty girls. How wonderful!

Input
The first line contain an integer T. Then T cases followed. Each case begins with two integers n(2 <= n <= 100000) , k(0 <= k < 2^31). And then the next line contain n integers.
Output
For each cases,output the numbers of solutions to the equation.
Sample Input
2 5 4 1 2 3 4 5 8 8 1 4 5 7 8 9 2 6
Sample Output
3 5
输入n,k;
输入n个数,在这n个数中找到两个数相加等于k的一共有几组?
注意:1.a+b与b+a,算两组;
2.连续两个相等的数字如n个数为(1,1,3,2,6),k=4;答案是3种;两个1只能看一个;
3.若用折半法提前排序;
代码:
折半法
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,k;
int a[100010];
int findx(int t)
{
int low=0,high=n-1,mid;
while(low<high)
{
mid = low+ (high-low)/2;
if(a[mid]==t)
return mid;
else if(a[mid]>t)
high=mid;
else
low=mid+1;
}
return -1;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&k);
int i,sum=0;
for(i = 0 ; i < n ; i ++)
scanf("%d",&a[i]);
sort(a,a+n);
for( i = 0 ; i < n ; i ++)
{
if(a[i]==a[i+1])//
continue;
else if(findx(k-a[i])!=-1)
sum++;
}
printf("%d\n",sum);
}
return 0;
}map
#include<stdio.h>
#include<math.h>
#include<map>
using namespace std;
int main()
{
map<int,int>m;
int n,k,t;
scanf("%d",&t);
while(t--)
{
m.clear();
scanf("%d%d",&n,&k);
int i,j,z,sum=0,x,s=0,l;
for(i=0;i<n;i++)
{
scanf("%d",&z);
m[z]=1;
}
map<int,int>::iterator it;
for(it=m.begin();it!=m.end();it++)
{
x=k-(*it).first;
if(m.find(x)!=m.end())
sum++;
}
printf("%d\n",sum);
}
return 0;
}set
#include<stdio.h>
#include<math.h>
#include<set>
using namespace std;
int main()
{
int n,k,t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&k);
set<int>ss;
int i,j,z,sum=0,x;
for(i=0;i<n;i++)
{
scanf("%d",&z);
ss.insert(z);
}
set<int>::iterator it;
for(it=ss.begin();it!=ss.end();it++)
{
x=k-*it;
if(ss.find(x)!=ss.end())
sum++;
}
printf("%d\n",sum);
}
return 0;
}
本文介绍了一个编程挑战问题,旨在寻找给定整数集合中所有可能的配对组合,使得这些配对的和等于特定的目标值。文章提供了三种不同的实现方案:使用折半查找、map以及set数据结构。

被折叠的 条评论
为什么被折叠?



