zoukankan      html  css  js  c++  java
  • Equation

    You are given an equation:

    Ax2 + Bx + C = 0.

    Your task is to find the number of distinct roots of the equation and print all of them in ascending order.

    Input

    The first line contains three integer numbers A, B and C ( - 105 ≤ A, B, C ≤ 105). Any coefficient may be equal to 0.

    Output

    In case of infinite root count print the only integer -1. In case of no roots print the only integer 0. In other cases print the number of root on the first line and the roots on the following lines in the ascending order. Print roots with at least 5 digits after the decimal point.

    Example

    Input
    1 -5 6
    Output
    2
    2.0000000000
    3.0000000000

    分类讨论。。如果根是0,单独讨论,不然会输出-0。
    代码:
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
        double a,b,c,det;
        scanf("%lf%lf%lf",&a,&b,&c);
        det = b * b - 4 * a * c;
        if(a == 0)
        {
            if(b == 0 && c == 0)printf("%d",-1);
            else if(b == 0)printf("%d",0);
            else if(c == 0)printf("%d
    %.10f",1,0);
            else printf("%d
    %.10f",1,-1.0 * c / b);
        }
        else if(b == 0 && c == 0)
        {
            printf("%d
    %.10f",1,0);
        }
        else if(det < 0)printf("%d",0);
        else if(det == 0)
        {
            printf("%d
    %.10f",1,-0.5 * b / a);
        }
        else
        {
            if(a > 0)printf("%d
    %.10f
    %.10f",2,0.5 / a * (- b - sqrt(det)),0.5 / a * (sqrt(det) - b));
            else printf("%d
    %.10f
    %.10f",2,0.5 / a * (sqrt(det) - b),0.5 / a * (- b - sqrt(det)));
        }
    }
  • 相关阅读:
    序列化器:serializers(django-rest-framework)
    数据库模型:models(Django)
    AtCoder Beginner Contest 213【A
    Codeforces Round #736 (Div. 2)【ABCD】
    AtCoder Beginner Contest 212【A
    Codeforces Round #732 (Div. 2)【ABCD】
    VS201X windows下编译提示缺少ucrtbased.dll文件
    Locust1.6 从入门到实战
    如何理解Windows认证流程
    HTB::Forest
  • 原文地址:https://www.cnblogs.com/8023spz/p/8439308.html
Copyright © 2011-2022 走看看