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 YESNO
给三个数组 每个数组取一个数 相加能否等于下面的例子
#include<cstdio> #include<algorithm> using namespace std; int a[501]; int b[501]; int c[501]; int d[505001]; int main() { int L,n,m,t,q; int sum=1; while(scanf("%d%d%d",&L,&n,&m)!=EOF) { for(int i=1;i<=L;i++) { scanf("%d",&a[i]); } for(int i=1;i<=n;i++) { scanf("%d",&b[i]); } for(int i=1;i<=m;i++) { scanf("%d",&c[i]); } sort(a+1,a+L+1); sort(b+1,b+n+1); sort(c+1,c+m+1); int cnt=0; for(int i=1;i<=L;i++) { for(int j=1;j<=n;j++) { d[++cnt]=a[i]+b[j]; } } sort(d+1,d+cnt+1); scanf("%d",&t); printf("Case %d:\n",sum++); while(t--) { int f=0; scanf("%d",&q); for(int i=1;i<=m;i++) { int l=1,r=cnt; while(l<=r) { int mid=(l+r)/2; if(d[mid]+c[i]==q) { f=1; break; } if(d[mid]+c[i]<q) { l=mid+1; } else { r=mid-1; } } if(f==1) { break; } } if(f==1) { printf("YES\n"); } else { printf("NO\n"); } } } return 0; }