发布网友 发布时间:2022-12-16 17:11
共1个回答
热心网友 时间:2024-11-23 20:54
#include#includefloat getequation(float x)
{
return x*x-2*x+2;
}
void main()
{
float a=0;
float b=6;
float result = 0;
do {
float c=a+0.618*(b-a);
float d=a+b-c;
if(fabs(getequation(c)) < fabs(getequation(d)))
{
a=d;
result = c;
}
else
{
b=c;
result = d;
}
} while(fabs(getequation(result)) > 0.01);
printf("f(a)=a*a-7*a+10\n");
printf("a=%f\n",result);
}
扩展资料
C语言编写程序求最大值
#include<stdio.h>
double Max(double*p,int n)
{
double max=*p;
for(int i=0;i<n;i++)
if(*(p+i)>max)
max=*(p+i);
return max;
}
double Min(double *p,int n)
{
double min=*p;
for(int i=0;i<n;i++)
if(*(p+i)<min)
min=*(p+i);
return min;
}
double Average(double *p,int n)
{
double sum=0;
for(int i=0;i<n;i++)
sum+=*(p+i);
return sum/n;
}
int main()
{
double a[10];
printf("请输入10个实数:\n");
for(int i=0;i<10;i++)
scanf("%f",&a[i]);
printf("这10个数中最大值为:%f\n",Max(a,10));
printf("这10个数中最小值为:%f\n",Min(a,10));
printf("这10个数的平均值为:%f\n",Average(a,10));
return 0;