[51nod] 1090 3个数和为0 暴力+二分
第1行,1个数N,N为数组的长度(0 <= N <= 1000)
第2 - N + 1行:A[i](-10^9 <= A[i] <= 10^9)
如果没有符合条件的组合,输出No Solution。
如果有多个,按照3个数中最小的数从小到大排序,如果最小的数相等则继续按照第二小的数排序。每行3个数,中间用空格分隔,并且这3个数按照从小到大的顺序排列。
7
-3
-2
-1
0
1
2
3
-3 0 3
-3 1 2
-2 -1 3
-2 0 2
-1 0 1 二重循环+二分 O(N^2LogN)
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std; int a[]; bool binary_find(int l, int r, int x)
{
while (l <= r) {
int m = (l+r)>>;
if (a[m] > x)
r = m - ;
else if (a[m] < x)
l = m + ;
else
return ;
}
return ;
} int main()
{
//freopen("1.txt", "r", stdin);
int n;
scanf("%d", &n);
for (int i = ; i < n; i++)
scanf("%d", &a[i]);
sort(a, a+n);
int flag = ;
for (int i = ; i < n; i++) {
for (int j = i+; j < n; j++) {
int x = -(a[i]+a[j]);
// printf("%d %d %d\n", a[i], a[j], x);
if (binary_find(j+, n-, x)) {
printf("%d %d %d\n", a[i], a[j], x);
flag = ;
}
}
}
if (!flag) printf("No Solution\n"); return ;
}
更快的二分 同时搜索两个数
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std; int a[]; int main()
{
//freopen("1.txt", "r", stdin);
int n;
scanf("%d", &n);
for (int i = ; i < n; i++)
scanf("%d", &a[i]);
sort(a, a+n); int flag = ;
for (int i = ; i < n; i++) {
int j, k, x;
j = i+;
k = n-;
while (j < k) {
x = a[i]+a[j]+a[k];
if (x < )
j++;
else if (x > )
k--;
else {
printf("%d %d %d\n", a[i], a[j], a[k]);
flag = ;
j++; k--;
}
}
}
if (!flag) printf("No Solution\n"); return ;
}
[51nod] 1090 3个数和为0 暴力+二分的更多相关文章
- 51Nod 1090 3个数和为0(暴力)
1090 3个数和为0 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 给出一个长度为N的无序数组,数组中的元素为整数,有正有负包括0,并互不相等.从 ...
- 51nod 1090 3个数和为0【二分】
1090 3个数和为0 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 给出一个长度为N的无序数组,数组中的元素为整数,有正有负包括0,并互不相等.从 ...
- 51Nod 1090 3个数和为0 set 二分优化
给出一个长度为N的无序数组,数组中的元素为整数,有正有负包括0,并互不相等.从中找出所有和 = 0的3个数的组合.如果没有这样的组合,输出No Solution.如果有多个,按照3个数中最小的数从小到 ...
- [51nod] 1267 4个数和为0 暴力+二分
给出N个整数,你来判断一下是否能够选出4个数,他们的和为0,可以则输出"Yes",否则输出"No". Input 第1行,1个数N,N为数组的长度(4 < ...
- 51Nod 1090 3个数和为0
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1090 思路:排序 三个for循环 但是要控制循环 不能从头开 ...
- 51nod——T1267 4个数和为0
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1267 题目描述 给出N个整数,你来判断一下是否能够选出4个数,他们的和 ...
- 1001 数组中和等于K的数对 1090 3个数和为0
二分查找.对数组每个V[i],在其中查找K-V[i],查找完成后修改v[i]避免重复输出 #include<iostream> #include<algorithm> #inc ...
- 51Nod 1267 4个数和为0 二分
给出N个整数,你来判断一下是否能够选出4个数,他们的和为0,可以则输出"Yes",否则输出"No".Input第1行,1个数N,N为数组的长度(4 <= ...
- 51nod 1267 4个数和为0
基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个整数,你来判断一下是否能够选出4个数,他们的和为0,可以则输出"Yes",否则输出&qu ...
随机推荐
- spring是什么
spring是一个容器,用于降低代码间的耦合度,根据不同的代码采用了ioc和aop这二种技术来解耦合. 比如转账操作:a用户少1000,b用户多1000.这是主业务逻辑 IOC 涉及到的事务,日志 ...
- beego 自定义模板函数
beego支持的模板函数不是很多,有时候前端展现数据的时候,要对数据进行格式化,所以要用到自定义模板函数 比如我的前端模板上有时间和模板大小这2个数据,原始数据都是int的时间戳和byte单位的数据, ...
- 网页静态化解决方案:Freemarker生成简单html页面
FreeMarker 是一个用 Java 语言编写的模板引擎,它基于模板来生成文本输出.FreeMarker与 Web 容器无关,即在 Web 运行时,它并不知道 Servlet 或 HTTP.它不仅 ...
- 用java实现一个简易编译器
- hdu3999-The order of a Tree (二叉树的先序遍历)
http://acm.hdu.edu.cn/showproblem.php?pid=3999 The order of a Tree Time Limit: 2000/1000 MS (Java/Ot ...
- 【总结整理】AI产品经理大会2017(转载)
从企业大数据到企业 AI | 易观智慧院院长 李智 1.AI 不是目的,而是要了解 AI 是什么,真正意义上的强人工智能在前沿领域尚未取得突破,暂时只能在影视文学作品中去思考人机关系.机器人三定律在未 ...
- MySQL5.6.35部署
1.下载软件 [root@localhost src]# wget -q http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glib ...
- Linux buffer and cache
A buffer is something that has yet to be "written" to disk. A cache is something that has ...
- UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 12: ordinal not in range(128)问题解决
今天在验证字符串是否包含的时候报错:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 12: ordinal n ...
- iOS基础教程:在建好的项目中加入CoreData[转]
这几天在做一个ios的小项目,项目中需要对数据进行基本的增删改查操作.于是就想用一把CoreData.但在创建项目初期,没有包含进CoreData.于是就在已建好的项目中加入CoreData.由于第一 ...