标题:
c语言折半查找下标,数从大到小排列
[打印本页]
作者:
liuda
时间:
2015-1-22 02:25
标题:
c语言折半查找下标,数从大到小排列
#include<stdio.h>//折半查找下标,数从大到小排列
void main()
{int i;
int d;
int m=0;//最大值坐标
int n=14;//最小值坐标
int k;
int p;
int o;
int q=16;
int a[15];//用到的变量是越来越多了
printf("please input 15 number from big to small and desired number:\n");
for(i=0;i<=14;i++)
{
scanf("%d",&a[i]);//先输入15个数
}
scanf("%d",&d);//再输入要查找的数
for(k=1;k<=4;k++)
{
q=q/2;//每比较一次坐标变化量折半
p=q-2;
if(d==a[(m+n)/2])
{
printf("the coordinate is %d\n",(m+n)/2);
o=1;
}
else if(d>a[(m+n)/2])
{
m=(m+n)/2-1-p;//这个6我将来是想换掉,因为没有可重复性
n=(m+n)/2-1;//将来这个l是要除以2的
}
else
{
m=(m+n)/2+1;
n=(m+n)/2+1+p;
}
}
if (o!=1)
{printf(" do not exist\n");}
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1