Problem Description
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.
Input
There 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.
Output
For 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
题意:查找在三个数组中能否找到等于X的数。
#include<stdio.h> #include<iostream> #include<string.h> #include<math.h> #include<algorithm> #include<queue> #include<string> #include<map> #include<stack> #define maxn 505 #define inf 0x3f3f3f3f #define ll long long using namespace std; ll a[maxn],b[maxn],c[maxn],x[maxn*maxn]; int main(){ int l,n,m,t=0; while(~scanf("%d%d%d",&l,&n,&m)){ memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(c,0,sizeof(c)); memset(x,0,sizeof(x)); for(int i=0;i<l;i++){ cin>>a[i]; } for(int j=0;j<n;j++)cin>>b[j]; for(int i=0;i<m;i++)cin>>c[i]; int nn;cin>>nn; sort(a,a+l); sort(b,b+n); sort(c,c+m);int p=0; for(int i=0;i<l;i++){ for(int j=0;j<n;j++){ x[p++]=a[i]+b[j]; } } sort(x,x+p);int q; printf("Case %d: ",++t); for(int i=0;i<nn;i++){ cin>>q; int flag=0; for(int j=0;j<m;j++){ int y=q-c[j]; int ans=*lower_bound(x,x+p,y); if(ans==y){flag=1;cout<<"YES"<<endl;break;} } if(flag==0)cout<<"NO"<<endl; } } }