当前位置:网站首页>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
边栏推荐
- Reread reconstruction
- 块级元素和行内元素
- 使用TreeView树型菜单栏(递归调用数据库自动创建菜单)
- Windows must be installed with efficiency software!
- SQL Chapter 2 Chapter 3
- Understanding data structures starts with this article~
- Open source ERP recruitment
- A simple way to realize terminal text paste board
- Android NDK development and actual combat WeChat official account 2-D code detection
- Android rights
猜你喜欢

In the future, China Telecom will make cloud computing service the main business of China Telecom

阿里、腾讯、百度、网易、美团Android面试经验分享,拿到了百度、腾讯offer

Android Development - service application, timer implementation (thread + service)

Fedora 33 Workstation 的新功能

Pay attention to the request forwarding problem of. Net core

JVM学习(六)-内存模型和线程

Solve the problem of idea shortcut key Alt + insert invalid

基于synchronized锁的深度解析

Visit Jingdong | members of Youth Innovation Alliance of China Academy of space technology visit Jingdong headquarters

20201107第16课,使用Apache服务部署静态网站;使用Vsftpd服务传输文件
随机推荐
【golang】GC详解
Kubernetes business log collection and monitoring
Three practical skills of Medical Project Management
iPhone“连到系统上的设备没有发挥作用”原因分析及解决方法 20200105
Open source ERP recruitment
Setting up a proxy for the WGet command
Four steps of Android integrated payment
Online course of tutorial system processing is in progress
Visit Jingdong | members of Youth Innovation Alliance of China Academy of space technology visit Jingdong headquarters
Show profile analysis of SQL statement performance overhead
android studio AIDL的使用
导师制Processing网课 双十一优惠进行中
实现商品CRUD操作
Well, the four ways to query the maximum value of sliding window are good
Interview summary on November 7, 2020 (interview 12K)
从编码、网络传输、架构设计揭秘腾讯云高质量、高可用实时音视频技术实践...
在嵌入式设备中实现webrtc的第三种方式③
From coding, network transmission, architecture design, Tencent cloud high quality, high availability real-time audio and video technology practice
彩虹排序 | 荷兰旗问题
Gather in Beijing! Openi / O 2020 Qizhi Developer Conference enters countdown