1 条题解
-
0
C :
#include <stdio.h> #include <math.h> /* 定义f函数,计算题目中的函数f(x)=x3-5x2+15x-80 */ float f(float x) { float y; y = ((x - 5.0) * x + 16.0) * x - 80.0; return y; } /* 定义xpoint函数,计算弦与x轴的交点 */ float xpoint(float x1, float x2) { float y; y = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1)); return y; } /* 定义root函数,计算近似的根 */ float root(float x1, float x2) { float x, y, y1; y1 = f(x1); do { x = xpoint(x1, x2); y = f(x); /* 如果f(x)与f(x1)同符号 */ if (y * y1 > 0) { y1 = y; x1 = x; } else { x2 = x; } } while (fabs(y) >= 0.0001); return x; } int main() { float x1, x2, x; scanf("%f%f", &x1, &x2); x = root(x1, x2); printf("%.4f\n", x); return 0; }
C++ :
#include <stdio.h> #include <math.h> /* 定义f函数,计算题目中的函数f(x)=x3-5x2+15x-80 */ float f(float x) { float y; y = ((x - 5.0) * x + 16.0) * x - 80.0; return y; } /* 定义xpoint函数,计算弦与x轴的交点 */ float xpoint(float x1, float x2) { float y; y = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1)); return y; } /* 定义root函数,计算近似的根 */ float root(float x1, float x2) { float x, y, y1; y1 = f(x1); do { x = xpoint(x1, x2); y = f(x); /* 如果f(x)与f(x1)同符号 */ if (y * y1 > 0) { y1 = y; x1 = x; } else { x2 = x; } } while (fabs(y) >= 0.0001); return x; } int main() { float x1, x2, x; scanf("%f%f", &x1, &x2); x = root(x1, x2); printf("%.4f\n", x); return 0; }
Pascal :
var x,x1,x2:double; // f(x)=x^3-5x^2+16x-80 function f(x:double):double; begin f:=((x-5)*x+16)*x-80; end; function xp(x1,x2:double):double; begin xp:=(x1*f(x2)-x2*f(x1)) / (f(x2)-f(x1)); end; function root(x1,x2:double):double; var x,y,y1:double; begin y1:=f(x1); repeat x:=xp(x1,x2); y:=f(x); if(y*y1>0) then begin //f(x)与f(x1)同号 y1:=y; x1:=x; end else x2:=x; //否则x作为x2 until abs(y)<1e-6; exit(x); end; begin readln(x1,x2); x:=root(x1,x2); writeln( x:0:4); end.
- 1
信息
- ID
- 1310
- 时间
- 1000ms
- 内存
- 32MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者