HDU-2141

Give you three sequences of numbers A, B, C, then we give you a number X. Now you need to calculate if you can find the three numbers Ai, Bj, Ck, which satisfy the formula Ai+Bj+Ck = X. 
InputThere are many cases. Every data case is described as followed: In the first line there are three integers L, N, M, in the second line there are L integers represent the sequence A, in the third line there are N integers represent the sequences B, in the forth line there are M integers represent the sequence C. In the fifth line there is an integer S represents there are S integers X to be calculated. 1<=L, N, M<=500, 1<=S<=1000. all the integers are 32-integers. 
OutputFor each case, firstly you have to print the case number as the form "Case d:", then for the S queries, you calculate if the formula can be satisfied or not. If satisfied, you print "YES", otherwise print "NO". 
Sample Input
3 3 3
1 2 3
1 2 3
1 2 3
3
1
4
10
Sample Output
Case 1:
NO
YES
NO

题意:就是给你3组数,然后再给你一组数,判断这组数中的数是否可以由那3组数中各一个的和。

AC代码为:可用把前两个数组合并然后让X减去第3组数与合并的数组比较每一个看是相等。

#include<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;


int main()
{
int L,N,M,S,num=1;
while(~scanf("%d%d%d",&L,&N,&M))
{

int a,b,c;
vector<int> v1,v2,v3,temp;

for(int i=0;i<L;i++)
{
scanf("%d",&a);
v1.push_back(a);
}

for(int i=0;i<N;i++)
{
scanf("%d",&b);
v2.push_back(b);
}

for(int i=0;i<M;i++)
{
scanf("%d",&c);
v3.push_back(c); 
}

for(int i=0;i<L;i++)
{
for(int j=0;j<N;j++)
{
temp.push_back(v1[i]+v2[j]);
}
}
sort(temp.begin(),temp.end());

scanf("%d",&S);
int flag[S],s1=0;
while(S--)
{
int x,sum=0;
scanf("%d",&x);

for(int i=0;i<v3.size();i++)
{
int xl=x-v3[i];
vector<int>::iterator it=lower_bound(temp.begin(),temp.end(),xl);
if(it!=temp.end() && *it==(x-v3[i]))
{
sum++;
}
}
if(sum)
flag[s1++]=1;
else
flag[s1++]=0;

}

printf("Case %d: ",num++);
for(int i=0;i<s1;i++)
{
if(flag[i])
{
printf("YES ");
}
else 
printf("NO ");
}


}

return 0;
}


原文地址:https://www.cnblogs.com/csushl/p/9386617.html