#include <stdio.h>
int search(int* nums,int size,int target);
int main()
{
int num[10]={};
int i=0,j=0,S=0,K=0;
printf("please input number!\n");
for(i=0;i<10;i++)
{
scanf("%d",&num[i]);
}
for(j=0;j<10;j++)
{
printf("num is %d\n",num[j]);
}
K=sizeof(num)/sizeof(int)-1;
S=search(num,K,22);
printf("K is %d\n",K);
if(S==-1)
printf("sorry,the target is not in num ");
else if(S==0)
printf("error");
else
printf("the target of num is %d",S);
return 0;
}
int search(int* nums,int size,int target)
{
int left=0;
int right=size;
int mid=1;
while(1)
{
if(nums[left]>nums[right])
return 0;
if(target>nums[right]||target<nums[left])
return -1;
mid=left+(right-left+1)/2;
if(nums[mid]>target)
{
right=mid-1;
}
else if(nums[mid]<target)
{
left=mid+1;
}
else if(nums[mid]=target)
return nums[mid];
}
}