Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calculate/printf with double @noi~/1.4~/#20求一元二次方程的根 #1

Open
sky-bro opened this issue Sep 14, 2019 · 0 comments
Open

Comments

@sky-bro
Copy link
Owner

sky-bro commented Sep 14, 2019

Origin: 20:求一元二次方程的根
can't get this right, my recent submitted code is:

#include <cstdio>
#include <cmath>

const double EPS = 1e-5;

int main(int argc, char const *argv[])
{
  double a, b, c;
  scanf("%lf%lf%lf", &a, &b, &c);
  double d = b*b;
  double e = 4*a*c;
  if (d>e) {
    double f = sqrt(e-d)/(2*a);
    printf("x1=%.5f;x2=%.5f\n", (-b+f)/(2*a), f, (-b-f)/(2*a), f);
  } else if (e>d) {
    double f = sqrt(e-d)/(2*a);
    double g = -b/(2.0*a);
    if (g < EPS && g > -EPS) g = 0.0;
    printf("x1=%.5f+%.5fi;x2=%.5f-%.5fi\n", g, f, g, f);
  } else {
    printf("x1=x2=%.5f\n", -b/(2*a));
  }
  return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant