折半查找的实现

1000(ms)
10000(kb)
2877 / 11213
编写程序实现折半查找算法。

输入

第一行是查找表的长度n
第二行是查找表中的数据元素 ;
第三行是要查找的数据元素的关键字.

输出

查找成功返回位序,不成功返回-1 ,第二行为比较的次数。

样例输入

11
5 13 19 21 37 56 64 75 80 88 92
100

样例输出

-1
4
 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int x;
int findx=-; int find(int arr[],int l,int r)
{
static int k=;
if(l>r)
return ;
int s=(l+r)>>;
if(arr[s]>x)
find(arr,l,s-);
else if(arr[s]<x)
find(arr,s+,r);
else
findx=s;
k++;
return k;
} int main()
{
int n;
cin>>n;
int *p=new int[n];
for(int i=;i<n;i++)
cin>>*(p+i);
cin>>x;
int y=find(p,,n-);
cout<<findx<<endl;
cout<<y;
delete(p);
return ;
}

swust oj 1010的更多相关文章

  1. [Swust OJ 404]--最小代价树(动态规划)

    题目链接:http://acm.swust.edu.cn/problem/code/745255/ Time limit(ms): 1000 Memory limit(kb): 65535   Des ...

  2. [Swust OJ 649]--NBA Finals(dp,后台略(hen)坑)

    题目链接:http://acm.swust.edu.cn/problem/649/ Time limit(ms): 1000 Memory limit(kb): 65535 Consider two ...

  3. SWUST OJ NBA Finals(0649)

    NBA Finals(0649) Time limit(ms): 1000 Memory limit(kb): 65535 Submission: 404 Accepted: 128   Descri ...

  4. [Swust OJ 1023]--Escape(带点其他状态的BFS)

    解题思路:http://acm.swust.edu.cn/problem/1023/ Time limit(ms): 5000 Memory limit(kb): 65535     Descript ...

  5. [Swust OJ 1125]--又见GCD(数论,素数表存贮因子)

    题目链接:http://acm.swust.edu.cn/problem/1125/ Time limit(ms): 1000 Memory limit(kb): 65535   Descriptio ...

  6. [Swust OJ 1126]--神奇的矩阵(BFS,预处理,打表)

    题目链接:http://acm.swust.edu.cn/problem/1126/ Time limit(ms): 1000 Memory limit(kb): 65535 上一周里,患有XX症的哈 ...

  7. [Swust OJ 1026]--Egg pain's hzf

      题目链接:http://acm.swust.edu.cn/problem/1026/     Time limit(ms): 3000 Memory limit(kb): 65535   hzf ...

  8. 九度OJ 1010:计算A+B【字符串和数组】

    /*======================================================================== 题目1010:A + B 时间限制:1 秒内存限制 ...

  9. [Swust OJ 1139]--Coin-row problem

    题目链接:  http://acm.swust.edu.cn/contest/0226/problem/1139/ There is a row of n coins whose values are ...

随机推荐

  1. js 本地缓存localStorage

    .localStorage - 没有时间限制的数据存储 ,,]; localStorage.setItem("stor",arr); console.log(localStorag ...

  2. 如何选择 Apache Tomcat 与 JDK 版本

    Apache Tomcat Version

  3. 服务器端 less的安装

    一. 安装 npm apt-get install npm 二. 安装less 在服务器端安装 LESS 的最简单方式就是通过 npm(node 的包管理器), 像这样: $ npm install ...

  4. MySQL 慢查询日志分析工具(pt-query-digest)

    1. 慢查询命令: 是否开启和日志路径:show variables like '%slow_query_log%'; 最大查询时间:show variables like '%query_time% ...

  5. struts2-第一章-基础用法2

    一,回顾 struts2框架搭建步骤 (1)创建maven项目 (2)web.xml配置struts2的核心过滤器(filter) (3)pom.xml配置servlet,struts的依赖 (4)把 ...

  6. linux条件判断:eq、ne、gt、lt、ge、le

    -eq(equal) :判断是否相等,相等为真 -ne(inequality):判断是否不等,不等为真 -gt(greter than):判断是否大于,大于为真 -lt(less than):判断是否 ...

  7. UWP Acrylic Material

    文档:https://docs.microsoft.com/en-us/windows/uwp/design/style/acrylic Acrylic 能带来类似 win7 的毛玻璃效果 要使用 A ...

  8. 3DMath

    线与面相交的计算 https://zh.wikipedia.org/wiki/%E7%BA%BF%E9%9D%A2%E4%BA%A4%E7%82%B9 什么是参数方程? 参数是参变数的简称.它是研究运 ...

  9. 当TFS/VSTS遇上Power BI

    引言 众所周知,要对TFS进行深入的图表分析,往往需要依赖于SQL Server Analysis Service和SQL Server Reporting Service.虽然随着TFS对敏捷项目的 ...

  10. 利用 SQL Server Audit 审核哪些用户添加删除更新SQL Agent Job

    有的时候我们需要下放权限给不用的用户,让他们自己能管理一部分SQL Agent Job,此时需要详细记录谁在什么时间修改了Job 甚至删除了Job, 我们可以使用SQL Server 的Audit帮助 ...