设为首页 加入收藏

TOP

二分查找的一个版本
2014-11-23 23:55:11 来源: 作者: 【 】 浏览:7
Tags:二分 查找 一个 版本

#include

#include

#define NUM 100

typedef struct node

{

int x;

int y;

}node ;

node global[NUM]={-999};

int count=0;

int find(int left,int right,node x)

{

int mid =(left +right )/2;

if (left==mid )

{

if( x.x==global [right].x)

return right;

else if( x.x==global [left].x)

return left;

else

return -1;

}

else if( x.x > global [ mid ].x )

return find ( mid ,right ,x );

else

return find (left , mid , x);

}

int main()

{

int i=0,j=0;

struct node temp;

for(i=0;i

{

j=i*i;

temp.x=j;

printf(" (%d) ",temp.x);

global[i].x=j;

}

printf("\n");

temp.x=0;

find ( 0,NUM-1,temp);

for(i=0;i

{

temp.x=global[i].x;

printf("result is {%d } \n", find ( 0 , NUM-1 , temp));

}

printf("new\n ");

while ( scanf("%d",&j) && j )

{

temp.x=j;

printf("result is {%d } \n", find ( 0 ,NUM-1 , temp));

}

}

摘自 chenbingchenbing的专栏

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇几个有意思的C语言程序 下一篇钻石运算符的一个 BUG

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: