P1024二分求50pts求纠错qwq
  • 板块学术版
  • 楼主__Cow__
  • 当前回复2
  • 已保存回复2
  • 发布时间2022/12/9 09:32
  • 上次更新2023/10/27 00:04:47
查看原帖
P1024二分求50pts求纠错qwq
589272
__Cow__楼主2022/12/9 09:32

我写的二分总是有点奇怪每次我都是枚举mid,输出mid,我看很多人会输出r,我也不晓得。。 反正看代码吧,我也很少写double是不是有精度问题也请指出

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
int step;
double mid,x1,x2,l,r,y,a,b,c,d;
double f (double x)
{
    return a*x*x*x+b*x*x+c*x+d;
}
int main()
{
    scanf ("%lf %lf %lf %lf", &a, &b, &c, &d);
    for (int i=-100;i<=99;i++)
    {
        l=i;
        r=i+1;
        x1 = f(l);
        x2 = f(r);
        if (x1==0)
        {
            printf ("%.2lf ", l);
	        step+=1;
            continue;
        }
        if (x1*x2<0)
        {
            while (r-l>=0.001)
            {
                mid = (l+r)/2;
                y = f(mid);
                if (y==0) 
                {
                    printf ("%.2lf ", mid);
                    step+=1;
                }
                if (mid>0) l=mid+0.01;
                else r=mid-0.01;
            }
        }
        if (step==3)
        {
            break;
        }
    }
    if (step!=3) printf ("100.00\n");
    else printf ("\n");
    return 0;
}

求求各位大佬了

2022/12/9 09:32
加载中...