用C语言来编进退法的搜索区间

Python015

用C语言来编进退法的搜索区间,第1张

用C语言来编进退法的搜索区间的源代码如下:

#include<stdio.h>

void main()

{

int a[20],x,i,start,end

printf("input 20 numbers:\n")

for(i=0i<20i++) scanf("%d",&a[i])

printf("please enter the number:\n")

scanf("%d",&x)

for(start=0,end=19start<=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、其实二分查找、插值查找以及斐波那契查找都可以归为一类——插值查找。插值查找和斐波那契查找是在二分查找的基础上的优化查找算法。

#include<stdio.h>

#include<math.h>

void main()

{

void function1()//搜索法

void function2()//二分法

void function4()//牛顿法

int choice

printf("请选择求解的方法:\n\t1.搜索法\n\t2.二分法\n\t3.牛顿法\n:")

switch(1)

{

case 1:function1()

case 2:function2()

case 4:function4()

}

}

void function1()//搜索法计算非线性方程的解

{

double expression1(double)

double lpoint=1.0,rpoint=2.0,step=0.0001

while(expression1(lpoint)<-0.00001)

{

lpoint=lpoint+step

}

printf("运用搜索法所求结果:%f\n",lpoint)

}

void function2()//二分法计算非线性方程的解

{

double expression1(double)

double lpoint=1,rpoint=2,mpoint

mpoint=(lpoint+rpoint)/2

while(fabs(expression1(mpoint))>0.00001)

{

mpoint=(lpoint+rpoint)/2

if(expression1(lpoint)*expression1(mpoint)<0)

rpoint=mpoint

else

lpoint=mpoint

}

printf("运用二分法所求结果:%f\n",mpoint)

}

void function4()//牛顿法计算非线性方程的解

{

double expression1(double)

double expression2(double)

double x=1.5

while(expression1(x)>0.00001)

{

x=x-expression1(x)/expression2(x)

}

printf("运用牛顿法所求结果:%f\n",x)

}

double expression1(double x)

{

double result

result=x*x*x-x*x-1

return result

}

double expression2(double x)

{

double result

result=3*x*x-2*x

return result