一、题目
二、代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int search(int* nums, int numsSize, int target) {
int left = 0;
int right = numsSize - 1;
while (left <= right) {
int middle = (left + right) / 2;
if (target > nums[middle]) {
left = middle + 1;
}
else if (target < nums[middle]) {
right = middle - 1;
}
else if(target==nums[middle])
{
return middle;
}
}
return -1;
}
int main()
{
int nums[] = { -1,0,3,5,9,12 };
int numsSize = sizeof(nums) / sizeof(nums[0]);
int target;
scanf("%d", &target);
int temp = search(nums, numsSize, target);
printf("%d", temp);
}
三、运行截图