当前位置:网站首页>c语言小白学习历程第六篇
c语言小白学习历程第六篇
2020-11-09 12:53:00 【osc_mra0q9h6】
学习目标:
难的咱也看不懂,先把简单的看了再说,加油L 冲冲冲!
学习时间:
2020年11月1日
学习产出:
c语言二分法实现数据查找
#include<stdio.h>
/******************
函数实现二分法
*******************/
int binary_research(int arr[],int length,int element)
{
int left = 0;
int right = 0;
right = length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (arr[mid] > element) {
right = mid - 1;
} else if {
(arr[mid] < element)
left = mid + 1;
} else {
return mid;
}
}
return -1;
}
int main()
{
int numStr[] = {
4, 7, 8, 45, 64, 123, 564, 586, 614, 688, 999};
int left = 0;
int right = 0;
int mid = 0;
int checkNum = 0;
int numLen = 0;
printf("可供查找的字符串是:\nnumStr[] = {4, 7, 8, 45, 64, 123, 564, 586, 614, 688, 999}\n");
scanf("%d", &checkNum);
numLen = sizeof(numStr) / sizeof(int);
//mid = binary_research(numStr, numLen, checkNum);
right = numLen - 1;
mid = (left + right) / 2;
// 普通方法实现二分发查找
while (left <= right) {
if (numStr[mid] > checkNum) {
right = mid -1;
} else if (numStr[mid] < checkNum) {
left = mid + 1;
} else {
break;
}
mid = (left + right) / 2;
}
printf("mid = %d\n", mid);
return 0;
}
版权声明
本文为[osc_mra0q9h6]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4267017/blog/4709446
边栏推荐
- Wechat circle
- 利用 Python 一键下载网易云音乐 10W+ 乐库
- 服务应用 ClockService安卓实现闹钟
- 天啦撸!打印日志竟然只晓得 Log4j?
- Front end code style practice prettier + eslint + git hook + lint staged
- JVM学习(四)-垃圾回收器和内存分配
- Android studio import customized framework classess.jar As 4.0.1 version is valid for pro test
- Implement crud operation
- SEO见风使舵,是对还是错?
- 注意.NET Core进行请求转发问题
猜你喜欢
10款必装软件,让Windows使用效率飞起!
Using rem, the font size changes when the screen zooms
在嵌入式设备中实现webrtc的第三种方式③
Detailed explanation of [golang] GC
Reduce of Flink
【分布式】分布式锁都有哪些实现方案?
技美那么贵,不如找顾问 | AALab企业顾问业务
Impact of libssl on CentOS login
Flink 系例 之 Reduce
Nine kinds of distributed primary key ID generation schemes of sub database and sub table are quite comprehensive
随机推荐
彩虹排序 | 荷兰旗问题
接口测试如何在post请求中传递文件
Safety (miscellany)
How to ensure that messages are not consumed repeatedly? (how to ensure the idempotent of message consumption)
In the future, China Telecom will make cloud computing service the main business of China Telecom
Suning's practice of large scale alarm convergence and root cause location based on Knowledge Map
android studio AIDL的使用
Rainbow sorting | Dutch flag problem
嗯,查询滑动窗口最大值的这4种方法不错....
Viewing PDB files from the angle of assembly
走进京东 | 中国空间技术研究院青年创新联盟成员莅临参观京东总部
The middle stage of vivo Monkey King activity
Where should wild card SSL certificate register and apply
Idea rest client, yes, I haven't opened postman yet
For and for... In, for each and map and for of
Oh, my God! Printing log only knows log4j?
Method of creating flat panel simulator by Android studio
Analysis of the source code of ThinkPHP facade
Handwriting Koa.js Source code
Windows must be installed with efficiency software!