如题所述
用C语言来编进退法的搜索区间的源代码如下:
#include<stdio.h>
void main()
{
int a[20],x,i,start,end;
printf("input 20 numbers:\n");
for(i=0;i<20;i++) scanf("%d",&a[i]);
printf("please enter the number:\n");
scanf("%d",&x);
for(start=0,end=19;start<=end;)
{
i=start+(end-start)/2;
if (x==a[i])
{
printf("%d",i+1);
getch();
return;
}
else if (x>a[i]) end = i-1;
else start=i+1;
}
扩展资料
1、C语言查找是在大量的信息中寻找一个特定的信息元素,在计算机应用中,查找是常用的基本运算,例如编译程序中符号表的查找。
2、其实二分查找、插值查找以及斐波那契查找都可以归为一类——插值查找。插值查找和斐波那契查找是在二分查找的基础上的优化查找算法。
温馨提示:答案为网友推荐,仅供参考
第1个回答 2013-05-27
#include<stdio.h>
#include<math.h>
#define f(t) (t*t-3*t)void sb(double *a,double *b)
{
double t0,t1,t,h,alpha,f0,f1;
int k=0;
printf("请输入初始点t0=");
scanf("%lf",&t0);
printf("\n请输入初始步长h=");
scanf("%lf",&h);
printf("\n请输入加步系数alpha(需大于1)=");
scanf("%lf",&alpha);
f0=f(t0);
t1=t0+h;
f1=f(t1);
while(1)
{
printf("\nf1=%lf,f2=%lf,t0=%lf,t=%lf,h=%lf,k=%d",f0,f1,t0,t1,h,k);
if(f1<f0)
{
h=alpha*h;
t=t0;
t0=t1;
f0=f1;
k++;
}
else
{
if(k==0)
{h=-h;t=t1;}
else
{
*a=t<t1?t:t1;
*b=t>t1?t:t1;
break;
}
}
t1=t0+h;
f1=f(t1);
}
}main()
{
double a=0,b=0;
double *c,*d;
c=&a,d=&b;
sb(c,d);
printf("\na=%lf,b=%lf",a,b);
}本回答被网友采纳
#include<math.h>
#define f(t) (t*t-3*t)void sb(double *a,double *b)
{
double t0,t1,t,h,alpha,f0,f1;
int k=0;
printf("请输入初始点t0=");
scanf("%lf",&t0);
printf("\n请输入初始步长h=");
scanf("%lf",&h);
printf("\n请输入加步系数alpha(需大于1)=");
scanf("%lf",&alpha);
f0=f(t0);
t1=t0+h;
f1=f(t1);
while(1)
{
printf("\nf1=%lf,f2=%lf,t0=%lf,t=%lf,h=%lf,k=%d",f0,f1,t0,t1,h,k);
if(f1<f0)
{
h=alpha*h;
t=t0;
t0=t1;
f0=f1;
k++;
}
else
{
if(k==0)
{h=-h;t=t1;}
else
{
*a=t<t1?t:t1;
*b=t>t1?t:t1;
break;
}
}
t1=t0+h;
f1=f(t1);
}
}main()
{
double a=0,b=0;
double *c,*d;
c=&a,d=&b;
sb(c,d);
printf("\na=%lf,b=%lf",a,b);
}本回答被网友采纳