#include<stdio.h>
int main()
{
int beg, mid,last,i,j,k=1 ,n;
int data[500] ;
int item ;
scanf("%d",&n) ;
for(i =0 ; i<n ; i++)
{
scanf("%d",&data[i]) ;
}
scanf("%d",&item) ;
beg = 0 ;
last = n-1 ;
while(beg<=last)
{
mid = (beg+last)/2 ;
if(item==data[mid])
{
break;
}
else if(item<data[mid])
{
last = mid-1 ;
}
else
{
beg = mid + 1 ;
}
}
if(beg>last)
{
printf("%d is not in the data",item) ;
}
else
{
printf("%d is in the data & it's position is %d",item,mid+1) ;
}
return 0;
}
int main()
{
int beg, mid,last,i,j,k=1 ,n;
int data[500] ;
int item ;
scanf("%d",&n) ;
for(i =0 ; i<n ; i++)
{
scanf("%d",&data[i]) ;
}
scanf("%d",&item) ;
beg = 0 ;
last = n-1 ;
while(beg<=last)
{
mid = (beg+last)/2 ;
if(item==data[mid])
{
break;
}
else if(item<data[mid])
{
last = mid-1 ;
}
else
{
beg = mid + 1 ;
}
}
if(beg>last)
{
printf("%d is not in the data",item) ;
}
else
{
printf("%d is in the data & it's position is %d",item,mid+1) ;
}
return 0;
}
No comments:
Post a Comment