#每日美图分享#
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int num = 0;
printf("please input:");
scanf("%d", &num);//想从数组中输出的数值
int arr[] = { 1,2,3,4,5,6,7,8,9,10};
int sz = sizeof(arr) / sizeof(arr[0]);
int left = 0;//最左下标
int right = sz-1;//最右下标
while (left <= right)
{
int mid = (left + right) / 2;
if (arr[mid] < num)
{
left = mid + 1;
}
else if (arr[mid] > num)
{
right = mid - 1;
}
else
{
printf("找着了,下标为:%d\n", mid);
break;
}
if (left > right)
printf("找不着了");
}
return 0;
}
今天先水一期,学了一天累????了。
先这样了哦,各位拜拜啦!