<代码收藏>二分查找

本文详细介绍了如何使用C++实现随机数组的生成与二分搜索算法,包括数组元素的随机初始化、二分查找过程及查找成功与失败的处理。
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

void main()
{
    srand(unsigned(time(NULL)));
	int size =rand()%50;
	int *p=new int[size];
	for(int i=0;i<size;++i)
	{
		if(i==0)
		{
			p[i]=rand()%300;
		}
		else
		{
			p[i]=p[i-1]+rand()%50;
		}

	}
	cout<<"array is "<<endl;
	for(int i=0;i<size;++i)
	{
		cout<<p[i]<<"\t";
	}
	cout<<endl;


	int number=0;
	cin>>number;

	int low=0;
	int high=size-1;
	int binary=(low+high)/2;
	while(number!=p[binary]&&low<=high)
	{
		if(number<p[binary])
		{
			high=binary-1;
		}
		else
		{
			low=binary+1;
		}
		binary=(low+high)/2;
	}
	if(low<=high)
	{
		cout<<"find successed"<<binary<<endl;
	}
	else
	{
		cout<<"can't find the number"<<endl;
	}


	int a;
	cin>>a;
    
}

D:\tools\python\python.exe D:\历史项目留存2\诺褀2025\python加工浦发模型模拟\py搭建\pythonProject1\爬虫抖音视频\测试01.py 小说下载器 - 自动翻页版 请输入小说起始URL: https://www.dbxsd.com/book/p23063/6291061.html 请输入输出文件名(默认: novel.txt): 2025-08-25 15:04:22,222 - INFO - 开始下载小说,起始URL: https://www.dbxsd.com/book/p23063/6291061.html 2025-08-25 15:04:22,223 - INFO - 基础URL: https://www.dbxsd.com 2025-08-25 15:04:22,223 - INFO - 正在下载第 1 章: https://www.dbxsd.com/book/p23063/6291061.html 2025-08-25 15:04:26,383 - INFO - 检测到编码: utf-8 2025-08-25 15:04:26,388 - INFO - 使用选择器: (‘div’, ‘content’) 找到内容容器 2025-08-25 15:04:26,398 - INFO - 通过分页区域找到下一页: https://www.dbxsd.com/book/p23063/6291061_2.html 2025-08-25 15:04:26,398 - INFO - 成功写入第 1 章内容 (1342 字符) 2025-08-25 15:04:26,398 - INFO - 正在下载第 2 章: https://www.dbxsd.com/book/p23063/6291061_2.html 2025-08-25 15:04:30,218 - INFO - 检测到编码: utf-8 2025-08-25 15:04:30,223 - INFO - 使用选择器: (‘div’, ‘content’) 找到内容容器 2025-08-25 15:04:30,226 - INFO - 通过JavaScript变量找到下一页: https://www.dbxsd.com/book/p23063/<a href= 2025-08-25 15:04:30,227 - INFO - 成功写入第 2 章内容 (65 字符) 2025-08-25 15:04:30,227 - INFO - 正在下载第 3 章: https://www.dbxsd.com/book/p23063/<a href= ================================================== 下载完成! 共 3 章,1407 字节 耗时: 12.07 秒 文件已保存至: D:\历史项目留存2\诺褀2025\python加工浦发模型模拟\py搭建\pythonProject1\爬虫抖音视频\novel.txt 2025-08-25 15:04:34,294 - ERROR - 错误: 无法获取页面,状态码: 404 2025-08-25 15:04:34,294 - WARNING - 检测到重复URL: https://www.dbxsd.com/book/p23063/<a href=,跳过 2025-08-25 15:04:34,294 - INFO - 已保存到: novel.txt 2025-08-25 15:04:34,294 - INFO - 总章节数: 3 2025-08-25 15:04:34,294 - INFO - 总字数: 1407 字节 2025-08-25 15:04:34,294 - INFO - 小说下载完成! Process finished with exit code 0 这个是翻页以后的页面源码 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>第339章 嘴巴里的出口(第2/2页)-《我的治愈系游戏》免费在线阅读_独步小说网</title> <meta name="keywords" content="独步小说网,我的治愈系游戏,第339章 嘴巴里的出口(第2/2页) 在线阅读"/> <meta name="description" content="当前章节:第339章 嘴巴里的出口(第2/2页)"/> <link href="/static/css/bootstrap.min.css" rel="stylesheet"> <link href="/static/css/font-awesome.min.css" rel="stylesheet" > <link href="/static/css/base.css" rel="stylesheet"> <script src="/static/js/action.js"></script> <!--[if lt IE 9]> <script src="/static/js/html5shiv.min.js"></script> <script src="/static/js/respond.min.js"></script> <![endif]--> <script type="text/javascript"> var book = new Object(); book.id="23063"; book.name="我的治愈系游戏"; book.author="我会修空调"; book.url="/book/p23063/"; book.chapterName="第339章 嘴巴里的出口(第2/2页)"; book.chapterUrl=window.location.href; book.chapterId="6291061"; </script> </head> <body> <div class="top"> <div class="container"> <div class="logo-name"><a href="/">独步小说网</a></div> <div class="float-right"><script>play("lan")</script></div> </div> </div> <div class="container"> <div class="search row"> <div class="col-sm-4 col-xs-12"> <form id="search-form" action="/plus/search.php"> <div class="input-group"> <input type="text" class="form-control" placeholder="请输入书名" id="bdcsMain" name="q" /> <div class="input-group-addon" onclick="search();"><span class="fa fa-search"></span></div> </div> </form> </div> <div class="col-sm-2 hidden-xs"> </div> <div class="col-sm-6 hidden-xs"> </div> </div> </div> <div class="container"> <div class="book-type t10"> <ol class="breadcrumb hidden-xs"> <li><a href="//www.dbxsc.com"><i class="fa fa-home"></i> 首页</a></li> <li><a href="/book/p23063/">我的治愈系游戏</a></li> <li class="active">第339章 嘴巴里的出口(第2/2页)</li> </ol> </div> <div class="cont-set clearfix"> <script>bookset();</script> </div> <!-- Content Begin--> <div class="content" id="content"> <h1 class="cont-title">第339章 嘴巴里的出口(第2/2页)</h1> <div class="row"> <div class="col-md-3"></div> <div class="col-md-6 text-center"> <a href="/book/p23063/6291061.html"><button type="button" class="btn btn-info"><i class="fa fa-arrow-circle-left" aria-hidden="true"></i> 上一页</button></a> <a href="/book/p23063/"><button type="button" class="btn btn-info">回目录</button></a> <a href="/book/p23063/6291062.html"><button type="button" class="btn btn-info">下一章 <i class="fa fa-arrow-circle-right" aria-hidden="true"></i></button></a> </div> </div> <div class="page"> <ul class="pagination pagination-sm"> <li><a href='6291061.html'>1</a></li><li class="active"><a>2</a></li> </ul> </div> <script>play("code-1");</script> <div id="cont-body" class="cont-body 6291061"> <script>play("code-2");</script> <p>“这就是出口?”</p> </div> <div class="clear"></div> <div class="note"><script>play("note");</script></div> <script>play("code-3");</script> <div class="page"> <ul class="pagination pagination-sm"> <li><a href='6291061.html'>1</a></li><li class="active"><a>2</a></li> </ul> </div> <script>play("code-4");</script> <div class="row t10"> <div class="col-md-3"></div> <div class="col-md-6 text-center"> <a href="/book/p23063/6291061.html"><button type="button" class="btn btn-info"><i class="fa fa-arrow-circle-left" aria-hidden="true"></i> 上一页</button></a> <a href="/book/p23063/"><button type="button" class="btn btn-info">回目录</button></a> <a href="/book/p23063/6291062.html"><button type="button" class="btn btn-info">下一章 <i class="fa fa-arrow-circle-right" aria-hidden="true"></i></button></a> </div> </div> <script>play("code-5");</script> </div> <!-- Content End--> </div> <!-- Footer Begin --> <div class="footer"> <div class = "container"> <p class = "hidden-xs">如果版权人认为在本站放置您的作品有损您的利益,请发邮件至<script>play("email")</script>,本站确认后将会无条件删除。</p> <p class = "hidden-xs">本站所收录作品、社区话题、书库评论均属其个人行为,不代表本站立场。</p> <p>有能力者,请一定订阅和购买正版书籍支持作者,这样作者才能写出更多更好的书!</p> <p class = "hidden-xs"> <script>play("tongji")</script> </p> </div> </div> <div class="fixed-btn"> <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a> </div> <!-- Footer End --> <!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> <script src="/static/js/jquery.min.js"></script> <!-- Include all compiled plugins (below), or include individual files as needed --> <script src="/static/js/bootstrap.min.js"></script> <script src="/static/js/comm.js"></script> <script type="text/javascript"> var shortname = ".html"; var npage = 2; var totalpage = 2; var namehand = "6291061"; var displaytype = "st"; var nexturl = "<a href='/book/p23063/6291062.html'><button type='button' class='btn btn-info'>下一章(→)</button></a>"; var preurl = "<a href='/book/p23063/6291060.html'><button type='button' class='btn btn-info'>(←)上一章</button></a>"; var muluurl = "/book/p23063/"; var cid = "6291061"; var sid ="23063"; play("flow"); </script> <script src="/static/js/article.js"></script> <script src="/static/js/up.js"></script> </body> </html>
最新发布
08-26
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值