1566. 4SUM

来源:互联网 发布:海岛奇兵坦升级数据 编辑:程序博客网 时间:2024/05/22 06:36

1566. 4SUM

Description

In this task, you are provided with four sets of integers. Your task consists of selecting one integer from each set, such that their sum is 0. You can assume that such selection exists.

Input

The first line of input contains four numbers, a, b, c, and d, separated by a space character, indicating the number of elements in each of the four sets. Each of these numbers is a positive integer 1 <= a, b, c, d <= 500. The following a + b + c + d lines contain the elements, each not smaller than −10000 and not larger than 10000. The elements of the first set are listed first, followed by the elements of the second set, etc.

Output

The output consists of the four integers, separated by a space character. The numbers must appear in the order in which they are listed in the input.

Sample Input

3 2 4 2517-8-13196-9100-147

Sample Output

17 -13 10 -14

暴力来,及时break就不会超时了。

#include <iostream>using namespace std;int main(){    int a,b,c,d,as[501],bs[501],cs[501],ds[501];    cin>>a>>b>>c>>d;    for(int i=0;i<a;i++)        cin>>as[i];    for(int i=0;i<b;i++)        cin>>bs[i];    for(int i=0;i<c;i++)        cin>>cs[i];    for(int i=0;i<d;i++)        cin>>ds[i];    bool judge=false;    for(int i=0;i<a;i++)    {        for(int j=0;j<b;j++)        {            for(int k=0;k<c;k++)            {                for(int l=0;l<d;l++)                {                    if(as[i]+bs[j]+cs[k]+ds[l]==0)                    {                        cout<<as[i]<<" "<<bs[j]<<" "<<cs[k]<<" "<<ds[l]<<endl;                        judge=true;                        break;                    }                }                if(judge==true) break;            }            if(judge==true) break;        }        if(judge==true) break;    }    return 0;}




原创粉丝点击