九度oj 题目1352:和为S的两个数字
- 题目描述:
- 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。
- 输入:
-
每个测试案例包括两行:第一行包含一个整数n和k,n表示数组中的元素个数,k表示两数之和。其中1 <= n <= 10^6,k为int第二行包含n个整数,每个数组均为int类型。
- 输出:
- 对应每个测试案例,输出两个数,小的先输出。如果找不到,则输出“-1 -1”
- 样例输入:
-
6 15
1 2 4 7 11 15
- 样例输出:
-
4 11 今天状态真是差劲
一开始的代码如下#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; int num[]; int main(int argc, char const *argv[])
{
int n, k;
while(scanf("%d %d",&n,&k) != EOF) {
for(int i = ; i < n; i++) {
scanf("%d",&num[i]);
}
int iptr = ;
int mina = -, minb = -;
while(iptr < n) {
int p = k - num[iptr];
int t = lower_bound(num, num + n, p) - num;
if(num[t] == p && t > iptr) {
mina = num[iptr];
minb = p;
break;
}
iptr++;
}
printf("%d %d\n",mina, minb);
}
return ;
}思路很简单,用二分搜索找到另一个值在数组中的位置。结果反复提交都是错误
问题:lower_bound返回的是num[i]>=p的指针的最小值,
如果输入
5 2
1 1 1 2 5就会出错
改正
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; int num[]; int main(int argc, char const *argv[])
{
int n, k;
while(scanf("%d %d",&n,&k) != EOF) {
for(int i = ; i < n; i++) {
scanf("%d",&num[i]);
}
int iptr = ;
int mina = -, minb = -;
while(iptr < n && num[iptr] * <= k) {
int p = k - num[iptr];
int t = upper_bound(num, num + n, p) - num - ;
if(num[t] == p && t > iptr) {
mina = num[iptr];
minb = p;
break;
}
iptr++;
}
printf("%d %d\n",mina, minb);
}
return ;
}其实此题还有一种更好的解法
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; int num[]; int main(int argc, char const *argv[])
{
int n, k;
while(scanf("%d %d",&n,&k) != EOF) {
for(int i = ; i < n; i++) {
scanf("%d",&num[i]);
}
int low = , high = n-;
int mina = -, minb = -;
while(low < high) {
if(num[low] + num[high] == k) {
mina = num[low];
minb = num[high];
break;
}
else if(num[low] + num[high] < k) {
low++;
}
else {
high--;
}
}
printf("%d %d\n",mina, minb);
}
return ;
}注意找到的第一个便是乘积最小的!!!
九度oj 题目1352:和为S的两个数字的更多相关文章
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 九度oj题目&吉大考研11年机试题全解
九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码). http://ac.jobdu.com/problem.php?pid=11 ...
- 九度oj 题目1007:奥运排序问题
九度oj 题目1007:奥运排序问题 恢复 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ题目1105:字符串的反码
tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...
- 九度oj题目1009:二叉搜索树
题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...
- 九度oj题目1002:Grading
//不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...
- 九度OJ题目1003:A+B
while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...
随机推荐
- 总结React关于require的问题
我需要实现的是当登录页面传过来的sex值为1则性别一栏的图片修改为boy.png如果为0性别图片则显示为girl.png‘ 最开始是这么写的为了让他成为变量 所以不行ok我们回到React的生命周期函 ...
- m_pConnection.CreateInstance( "ADODB.Connection ") 执行错误 结果总是为NULL
今天下午搞了下项目 数据库操作模块,总是出现m_pConnection.CreateInstance( "ADODB.Connection ") 执行错误,即m_pConnecti ...
- Android 视频录制 java.lang.RuntimeException: start failed.
//mRecorder.setVideoSize(320, 280); // mRecorder.setVideoFrameRate(5); mRecorder.setOutputFile(viodF ...
- Spring MVC系列[2]——参数传递及重定向
1.目录结构 2.代码 <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ...
- mybatis 部署日志
<!-- 打印查询语句 --> <setting name="logImpl" value="STDOUT_LOGGING" /> &l ...
- 最完整的台达PLC培训教程(沈阳工大)学习笔记1
1) 可编程控制器的应用1 开关量逻辑控制:电动机启动与停止2 运动控制:对步进电动机或伺服电动机的单轴或多轴系统实现位置控制3 过程控制:对温度.压力.流量等连续变化的模拟量进行闭环控制4 数据处理 ...
- Unity3d中MonoBehavior默认函数的执行顺序和生命周期
Awake()在MonoBehavior创建后就立刻调用,在脚本实例的整个生命周期中,Awake函数仅执行一次:如果游戏对象(即gameObject)的初始状态为关闭状态,那么运行程序,Awake函数 ...
- spark 的RDD各种转换和动作
今天先把spark的各种基本转换和动作总结下,以后有时间把各种用法放上去. 1 RDD基本转换操作 map.flagMap.distinct coalesce.repartition coale ...
- git 添加 ,密匙
转载此处 https://blog.csdn.net/xiayiye5/article/details/79652296
- LeetCode || 双指针 / 单调栈
11. Container With Most Water 题意:取两根求最大体积 思路:使用两个指针分别指向头和尾,然后考虑左右两根: 对于小的那根,如果选择了它,那么能够产生的最大体积一定是当前的 ...