

在一个有序数组中查找具体数字。
source link: https://blog.51cto.com/u_15797078/5706889
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在一个有序数组中查找具体数字。
精选 原创要求:查找数组中某一具体数字,并且输出打印其下标。
算法:折算查找算法(二分查找算法)
左下标:int left=0;
右下标:int right=sz-1;
数组中数字个数sz=sizeof(arr)/sizeof(arr[0]);
数组:char arr[]={1,2,3,4,5,6,7,8,9,10};
int med=(right+left)/2;




#include<stdio.h>
int main()
char arr[]={1,2,3,4,5,6,7,8,9,10};
int b=0;
int sz=sizeof(arr)/sizeof(arr[0]);
int left=0;
int right=sz;
printf("请输入要找的数字(1-10以内):");
scanf("%d",&b);
int med=(left+right)/2;
if(arr[med]<b)
left=med+1;
right=med-1;
printf("已找到,其对应的下标为:%d\n",med);
break;
if(left>right)
printf("找不到\n");
注:要了解具体的算法,才能写好代码。
- 1赞
- 收藏
- 评论
- 分享
- 举报
Recommend
-
33
题目来源: 力扣(LeetCode) 题目详情: 给你两个有序整数数组 nums1 和 nums2,请你将 nums...
-
11
如何去除有序数组的重复元素¶ 本网站的颜值和响应速度大幅优化,👉
-
9
LeetCode 第 167 号问题:两数之和 II – 输入有序数组-程序员小吴 当前位置:程序员小吴 > LeetCodeAnimation > LeetCode 第 167 号问题:两数...
-
4
Leetcode 26. 删除有序数组中的重复项 2021-06-21 26. 删除有序数组中的重复项哎,闲着无事的时候找了个简单题...
-
11
Leetcode 88 合并两个有序数组发布于 今天 15:13 给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1...
-
4
#yyds干货盘点#Leetcode 26. 删除有序数组中的重复项 原创 java厂长 2022-03-10 18:28:44...
-
1
删除有序数组中的重复项~~ 原创 掘金安东尼 2022-03-24 11:42:12...
-
10
leetcode 4. 寻找两个有序数组的中位数 发表于...
-
3
LeetCode 第 26 题: 删除有序数组中的重复项func removeDuplicates(nums []int) int { n_l := len(nums) if n_l == 0 { return 0 }k := 0 for i := 1; i< n_l; i++{
-
6
1 有序查找 查找就是根据给定的某个值,在查找表中确定一个其关键字等于给定值的数据元素。 查找表按照操作方式分成两大种: 静态查找表(Static Search Table):只作查找操作的查找表 查询某个“特定...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK