1 条题解

  • 0
    @ 2025-2-14 20:44:41

    C :

    #include <stdio.h>
    #include <math.h>
    
    double hanshu(float x);
    
    int main()
    {
    	double j, k, x1, x2, x0;
    	scanf("%lf%lf", &x1, &x2);
    /*	while (hanshu(x1)*hanshu(x2) > 0)
    		scanf("%lf%lf", &x1, &x2);
    */
    	while (fabs(hanshu(x0)) > 1e-6)
    	{
    		x0 = (x1+x2) / 2;
    		if (hanshu(x0)*hanshu(x1) < 0)
    			x2 = x0;
    		else
    			x1 = x0;
    	}
    	printf("%.6f\n", x0);
    }
    
    double hanshu(float x)
    {
    	double y;
    	y = 2*pow(x, 3) - 4*pow(x, 2) + 3*x - 6;
    	return y;
    }
    

    C++ :

    #include<stdio.h>
    #include<math.h>
    
    const double eps=1e-6;
    
    double fun(double x)
    {
    	return 2*x*x*x-4*x*x+3*x-6;
    }
    
    int main()
    {
    	double a,b;
    	while(scanf("%lf%lf",&a,&b)!=EOF)
    	{
    		double mid;
    		while(fabs(a-b)>eps)
    		{
    			mid=(a+b)/2;
    			if(fun(mid)*fun(a)>0)
    				a=mid;
    			else if(fun(mid)*fun(b)>0)
    				b=mid;
    			else
    				break;
    		}
    		printf("%lf\n",mid);
    	}
    	return 0;
    }
    
    • 1

    信息

    ID
    361
    时间
    1000ms
    内存
    12MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者