1566. 4SUM

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 2
5
17
-8
-13
19
6
-9
10
0
-14
7

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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值