#include <stdio.h>
#include <stdlib.h>
#define n 6
/* 折半查找法找出该数是数组中第几个元素的值,如果不在数组中,则输出”无此数”*/
int main(int argc, char *argv[]) {
int a[n]={1,3,5,7,9,10};// 012345 5/2=2 4<5 and mid-1 //(high= 1 low=0 and mid =0)
// int i;
int high=n-1,low=0;
int mid=(high+low)/2;
int num;//the num is the input number
scanf("%d",&num);
while(low<=high) //0<5 mid=2,3<a[2]=5, high=1, ||2.and mid=0 low=0,high=1 3>1 3.low=mid+1=1 high=1 and the mid =1 finally 3==3 printf("the mark is 1")
{
if(num>a[mid])
{
low=mid+1;
}
if(num<a[mid])
{
high=mid-1;
}
if(num==a[mid])
{
printf(" the mark is %d\n",mid);
break;// fine it ,so stop down //
}
mid=(low+high)/2;
}
if(low>high)
printf("not exist!");
return 0;
}
#include <stdlib.h>
#define n 6
/* 折半查找法找出该数是数组中第几个元素的值,如果不在数组中,则输出”无此数”*/
int main(int argc, char *argv[]) {
int a[n]={1,3,5,7,9,10};// 012345 5/2=2 4<5 and mid-1 //(high= 1 low=0 and mid =0)
// int i;
int high=n-1,low=0;
int mid=(high+low)/2;
int num;//the num is the input number
scanf("%d",&num);
while(low<=high) //0<5 mid=2,3<a[2]=5, high=1, ||2.and mid=0 low=0,high=1 3>1 3.low=mid+1=1 high=1 and the mid =1 finally 3==3 printf("the mark is 1")
{
if(num>a[mid])
{
low=mid+1;
}
if(num<a[mid])
{
high=mid-1;
}
if(num==a[mid])
{
printf(" the mark is %d\n",mid);
break;// fine it ,so stop down //
}
mid=(low+high)/2;
}
if(low>high)
printf("not exist!");
return 0;
}