欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Dating with girls(1)

程序员文章站 2022-06-02 11:52:30
...
Dating with girls(1)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2578
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2792 Accepted Submission(s): 886

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!
Dating with girls(1)

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

代码:

#include<stdio.h>
#include<string.h>

int main()
{
    int m,a,i,s;
    long int b,c[100010],lu[100010];
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d%ld",&a,&b);
        memset(lu,0,sizeof(lu));
        s=0;
        for(i=0; i<a; i++)
        {
            scanf("%ld",&c[i]);
            lu[c[i]]=1;//将数组中的数都标记;
        }
        for(i=0; i<a; i++)
        {
            if(lu[c[i]]&&lu[b-c[i]])//当这两个数都在所给的数组中时;
            {
                if(b-c[i]==c[i])//所找的两个数相同时,其实只是一个数;
                    s+=1;
                else 
                    s+=2;
                lu[c[i]]=lu[b-c[i]]=0;//取消标记;
            }
        }
        printf("%d\n",s);
    }
}

相关标签: 水题