当前位置:网站首页>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
边栏推荐
- 医疗项目管理的三种实用技巧
- An attempt to read or write to protected memory occurred using the CopyMemory API. This usually indicates that other memory is corrupted.
- Windows must be installed with efficiency software!
- 10款必装软件,让Windows使用效率飞起!
- 注意.NET Core进行请求转发问题
- IDEA rest-client,会了它我还没打开过postman
- Adobe experience design / XD 2020 software installation package (with installation tutorial)
- iPhone“连到系统上的设备没有发挥作用”原因分析及解决方法 20200105
- Three practical skills of Medical Project Management
- Viewing PDB files from the angle of assembly
猜你喜欢

android studio AIDL的使用

Android NDK 开发实战 - 微信公众号二维码检测

Tidb x micro banking reduces time consumption by 58%, and distributed architecture helps to realize inclusive finance

IDEA rest-client,会了它我还没打开过postman

SEO见风使舵,是对还是错?

基于synchronized锁的深度解析

Technology and beauty are so expensive, it's better to find consultants | aalab enterprise consulting business

Android studio AVD

嗯,查询滑动窗口最大值的这4种方法不错....

Rainbow sorting | Dutch flag problem
随机推荐
New features of Fedora 33 workstation
利用 Python 一键下载网易云音乐 10W+ 乐库
How to use function framework to develop large web application
Impact of libssl on CentOS login
Go语言初始化单例变量的几种方法
Depth analysis based on synchronized lock
分库分表的 9种分布式主键ID 生成方案,挺全乎的
AI fresh student's annual salary has increased to 400000, you can still make a career change now!
Biden wins the US election! Python developers in Silicon Valley make fun of Ku Wang in this way
Oh, my God! Printing log only knows log4j?
Glsb involves load balancing algorithm
Interview summary on November 7, 2020 (interview 12K)
What are the implementations of distributed locks?
关于无相互作用极化率的计算
手写Koa.js源码
Introduction to zero based im development (4): what is message timing consistency in IM systems?
Kubernetes business log collection and monitoring
IDEA rest-client,会了它我还没打开过postman
Tidb x micro banking reduces time consumption by 58%, and distributed architecture helps to realize inclusive finance
通配符SSL证书应该去哪里注册申请