当前位置:网站首页>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
边栏推荐
- VisualStudio(Mac)安装过程笔记
- Kubernetes business log collection and monitoring
- vscode 插件配置指北
- Using stream to read and write files to process large files
- Safety (miscellany)
- 跟我一起学.NetCore之EF Core 实战入门,一看就会
- Android rights
- The choice of domain name of foreign trade self built website
- 在企业的降本增效诉求下,Cube如何助力科盾业务容器化“一步到位”?
- How to use function framework to develop large web application
猜你喜欢
嗯,查询滑动窗口最大值的这4种方法不错....
阿里、腾讯、百度、网易、美团Android面试经验分享,拿到了百度、腾讯offer
Android Studio Avd「真·小白食用方法」
EFF 认为 RIAA 正在“滥用 DMCA”来关闭 YouTube-DL
深圳C1考证历程
SEO见风使舵,是对还是错?
Oh, my God! Printing log only knows log4j?
配置交换机Trunk接口流量本地优先转发(集群/堆叠)
Handwriting Koa.js Source code
Configure switch trunk interface traffic local priority forwarding (cluster / stack)
随机推荐
注意.NET Core进行请求转发问题
Tidb x micro banking reduces time consumption by 58%, and distributed architecture helps to realize inclusive finance
JVM学习(六)-内存模型和线程
Reduce of Flink
Ali, Tencent, Baidu, Netease, meituan Android interview experience sharing, got Baidu, Tencent offer
深圳C1考证历程
Windows must be installed with efficiency software!
Android Studio Avd「真·小白食用方法」
Glsb involves load balancing algorithm
Large scale project Objective-C - nsurlsession access SMS verification code application example sharing
Open source ERP recruitment
关于无相互作用极化率的计算
Complete set of linked list operations of data structure and algorithm series (3) (go)
Rainbow sorting | Dutch flag problem
Idea rest client, yes, I haven't opened postman yet
Setting up a proxy for the WGet command
Viewing PDB files from the angle of assembly
Aren't you curious about how the CPU performs tasks?
Using rem, the font size changes when the screen zooms
手写Koa.js源码