四数之和

Time Limit: 2000/1000ms (Java/Others)

Problem Description:

有n个不同的整数,判断能否从中选4次,4个数和刚好为m。数字可重复选取

Input:

输入包含多组测试,每组测试第一行是两个数n(1<=n<=)和m(0<=m<=10^9)。
第二行是n个数a1,a2,a3...an(0<=ai<=10^8);

Output:

对于每组测试,如果能从中找出4个数和为m,则输出YES,否则输出NO。

Sample Input:

5 10
1 2 3 4 5
3 10
1 3 5
3 9
1 3 5

Sample Output:

YES
YES
NO
解题思路:因为四数和中选取的每个数字可以重复,因此我们可以先将每每两个数的和存放到set容器中(注意元素具有唯一性即不重复性),然后使用find()来查找set中是否有m-*it这个元素即可,思路清晰,代码可以过,不超时!忽略系数,此解法时间复杂度是O(n^2)。
AC代码:
 #include<bits/stdc++.h>
using namespace std;
int a[],n,m;
set<int> t;
bool findt(int obj){ //find()返回迭代器在容器中的位置
return t.find(obj)==t.end();
}
int main()
{
while(cin>>n>>m){
t.clear();
for(int i=;i<n;++i)
cin>>a[i];
for(int i=;i<n;++i)
for(int j=;j<n;++j)
t.insert(a[i]+a[j]); //每每两个数相加
bool flag=false; //标记set中是否有这个元素
for(set<int>::iterator it=t.begin();it!=t.end();++it)
if(!findt(m-*it)){flag=true;break;}
if(flag)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return ;
}

ACM_四数之和的更多相关文章

  1. LeetCode第十八题-四数之和

    4Sum 问题简介:定n个整数和整数目标的数组nums,是否有元素a,b,c,d在nums中,使a+b+c+d=target? 举例: 给定数组 nums = [1, 0, -1, 0, -2, 2] ...

  2. LeetCode18. 四数之和

    LeetCode18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值 ...

  3. [LeetCode] 454. 4Sum II 四数之和II

    Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such t ...

  4. LeetCode:四数之和【18】

    LeetCode:四数之和[18] 题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c ...

  5. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  6. 【LeetCode】四数之和

    [问题]给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找 ...

  7. LeetCode:两数之和、三数之和、四数之和

    LeetCode:两数之和.三数之和.四数之和 多数之和问题,利用哈希集合减少时间复杂度以及多指针收缩窗口的巧妙解法 No.1 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在 ...

  8. 【LeetCode】18.四数之和

    题目描述 18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 t ...

  9. Java实现 LeetCode 18 四数之和

    18. 四数之和 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target ...

随机推荐

  1. Codeforces 631A Interview【模拟水题】

    题意: 模拟模拟~~ 代码: #include<iostream> using namespace std; const int maxn = 1005; int a[maxn], b[m ...

  2. bzoj2212 Tree Rotations

    被BZOJ坑了一下午,原以为是我程序有问题一直WA,结果是我数组小了...为啥不给我RE!!! 线段树合并,对于逆序对而言,只能通过交换左右子树来达到,那么我们就可以想到对于一个结点而言,我们当然要取 ...

  3. Redis的集群方案之Sentinel(哨兵模式)(待实践)

    哨兵模式是主从切换的一种方案,但是可以借助此方案实现集群,达到高可用. 先收集教程,待实践. 参考: https://redis.io/topics/sentinel(官方文档) http://ife ...

  4. E-SATA接口

    1.VGA接口 VGA接口就是显卡上输出模拟信号的接口,也叫D-Sub接口,其实就是显示转移的接口,比如连接投影仪.连接电视等等.从外观上讲,VGA接口是一种D型接口,上面共有15针空,分成三排,每排 ...

  5. MySQL架构优化实战系列4:SQL优化步骤与常用管理命令

  6. 二 hbase

    Hbase 本文介绍Hbase.但本文的前提是假设你已经读过Google的BigTable论文. Introduction Hbase 是基于Google Big Table用java实现的分布式,列 ...

  7. MapReduce Cross 示例

    MapReduce Cross 示例 package com.bsr.cross; import java.io.IOException; import org.apache.hadoop.conf. ...

  8. Windows 2008 R2 SP1部署WSUS 3.0 SP2

    1 实验环境 1)域: 域名为fengxja.com: 网段:192.168.0网段,不连接外网. 域功能级别和林功能级别为Windows server 2003模式. 2)DC服务器: 域控制器: ...

  9. Excel数据字典转换为PDM(且显示表名、字段相应的中文描写叙述)

    在工作中遇到了一个问题就是把Excel数据字典转换为PDM. 可是转换完毕了全是英文,原来对表名.字段名的中文描写叙述就没有了. 且对于这个问题在powerdesigner15.2以后能够直接完毕.可 ...

  10. php生成二维码2

    <?php include "phpqrcode.php"; $value = "http://huizhongda.taobao.com/"; $err ...