1 solutions

  • 0
    @ 2024-12-11 0:09:07

    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

    Information

    ID
    669
    Time
    1000ms
    Memory
    12MiB
    Difficulty
    (None)
    Tags
    # Submissions
    0
    Accepted
    0
    Uploaded By