1001 数组中和等于K的数对 1090 3个数和为0
二分查找。对数组每个V[i],在其中查找K-V[i],查找完成后修改v[i]避免重复输出
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstdio>
using namespace std;
typedef long long LL;
#define INF 0x3f3f3f3f
int main()
{
vector<LL> vv;
bool sign = false;
LL t,k,n;
scanf("%lld%lld",&k,&n);
for(int i=;i<n;i++)
{
scanf("%lld",&t);
vv.push_back(t);
}
sort(vv.begin(),vv.end());
int l =vv.size();
for(int i=;i<l;i++)
{
if(vv[i]==INF) continue;
vector<LL>::iterator it;
if(binary_search(vv.begin(),vv.end(),k-vv[i]))
it = lower_bound(vv.begin(),vv.end(),k-vv[i]);
else
continue;
if(it!=vv.end()&&*it!=vv[i])
{
sign = true;
if(*it>vv[i])
printf("%lld %lld\n",vv[i],*it);
else
printf("%lld %lld\n",*it,vv[i]);
*it = INF;
}
}
if(!sign)
printf("No Solution\n");
return ;
}
三个数的和为0,在前面代码基础上略作修改即可。
#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long LL;
#define MAXN 1001
#define INF 0x3f3f3f3f
bool been[MAXN];
bool all_sign = false;
LL n;
vector<LL> VV;
void solve(LL k,vector<LL> vv,LL pos)
{
memset(been,false,sizeof(been));
for(LL i=pos+;i<n;i++)
{
if(been[i]==true) continue;
vector<LL>::iterator it;
if(binary_search(vv.begin()+pos+,vv.end(),k-vv[i]))
it = lower_bound(vv.begin()+pos+,vv.end(),k-vv[i]);
else
continue;
if(it!=vv.end()&&*it!=vv[i])
{
all_sign = true;
if(*it>vv[i])
printf("%lld %lld %lld\n",-k,vv[i],*it);
else
printf("%lld %lld %lld\n",-k,*it,vv[i]);
been[it-vv.begin()] = true;
}
} }
int main()
{
LL temp,i;
scanf("%lld",&n);
for(i=;i<n;i++)
{
scanf("%lld",&temp);
VV.push_back(temp);
}
sort(VV.begin(),VV.end());
for(i=;i<n;i++)
{
if(VV[i]>=)
break;
temp = VV[i];
solve(-temp,VV,i);
been[i] = true;
}
if(!all_sign)
printf("No Solution\n");
return ;
}
1001 数组中和等于K的数对 1090 3个数和为0的更多相关文章
- 1001 数组中和等于K的数对
1001 数组中和等于K的数对 基准时间限制:1 秒 空间限制:131072 KB 给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K的数对.例如K = 8,数组A ...
- 1001 数组中和等于K的数对 1002 数塔取数问题 1003 阶乘后面0的数量 1004 n^n的末位数字 1009 数字1的数量
1001 数组中和等于K的数对 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K ...
- 51nod 1001 数组中和等于K的数对【二分查找/排序】
1001 数组中和等于K的数对 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组 ...
- 51Nod 1001 数组中和等于K的数对 And 1015 水仙花数
1001 数组中和等于K的数对 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K ...
- 51Nod 1001 数组中和等于K的数对 Label:Water
给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K的数对.例如K = 8,数组A:{-1,6,5,3,4,2,9,0,8},所有和等于8的数对包括(-1,9),(0 ...
- 51Nod 1001 数组中和等于K的数对 Set
给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K的数对.例如K = 8,数组A:{-1,6,5,3,4,2,9,0,8},所有和等于8的数对包括(-1,9),(0 ...
- 51Nod 1001 数组中和等于K的数对
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1001一开始的想法是排序后二分搜索,发现会进行非常多不必要的遍历,十分耗时 ...
- 51Nod 1001数组中和等于K的数对
Input示例 8 9 -1 6 5 3 4 2 9 0 8 Output示例 -1 9 0 8 2 6 3 5 first try: #include "bits/stdc++.h&quo ...
- 51Nod - 1001:数组中和等于K的数对
基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K的数对.例如K = 8,数组A: ...
随机推荐
- awk使用说明
原文地址:http://www.cnblogs.com/verrion/p/awk_usage.html Awk使用说明 运维必须掌握的三剑客工具:grep(文件内容过滤器),sed(数据流处理器), ...
- 如何在虚拟机里安装Linux
本篇仅为作业... 实验课程:Linux 实验机器:联想y410p 指导老师:刘臣奇 实验时间:2016年9月25日 学生学号:140815 姓名:杨文乾 一.先安装虚拟机,之后创建一个新的虚拟机 之 ...
- js正则表达式校验非负浮点数:^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- c#中get set 的使用
在早期学习c#的过程中,经常遇到这样的语句: public string StudentName { get{return stuName;} set{stuName=value;} } 当时也不是很 ...
- 物联网框架SuperIO 2.2.9和ServerSuperIO 2.1同时更新,更适用于类似西门子s7-200发送多次数据,才能读取数据的情况
一.解决方案 二.更新内容 1.修改IRunDevice接口,把void Send(io,bytes)改成int Send(io,bytes).2.修改网络控制器,发送数据不直接使用IO实例,改为使用 ...
- Quartz框架(第一版)
任务调度 在企业级应用中,经常会制定一些"计划任务",即在某个时间点做某件事情 核心是以时间为关注点,即在一个特定的时间点,系统执行指定的一个操作 任务调度涉及多线程并发.线程池维 ...
- session 存入数据库 php
session 机制 1.php中session的生成机制 session是保存在服务器的,当我们在代码中调用session_start();时,PHP会同时往SESSION的存放目录(默认为/tm ...
- Ubantu【第一篇】:Ubantu中openssh连接
h3 { color: rgb(255, 255, 255); background-color: rgb(30,144,255); padding: 3px; margin: 10px 0px } ...
- JQuery 了解
jQuery是什么?为什么是这样?怎么用? jQuery是对JavaScript的一种封装,是一个常用功能库.javascript是做什么的jquery就是做啥的.用它主要使写js更简便而强大,有些功 ...
- Windows API 函数列表 附帮助手册
所有Windows API函数列表,为了方便查询,也为了大家查找,所以整理一下贡献出来了. 帮助手册:700多个Windows API的函数手册 免费下载 API之网络函数 API之消息函数 API之 ...