解题思路:读取输入,然后解方程,最后比较两个根,我就忘记比较了

注意事项:本次发i心安了一个问题,就是这个? :运算符,后边的语句要带括号x1 > x2 ?  :temp = x1, x1 = x2, x2 = temp;假如我这样写,答案就是错误的,希望引以为戒,我港式不知道哪里出了问题找了半天。

参考代码:

#include<iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int main()
{
    double a, b, c;
    double x1, x2, dert;
    double temp;
    cin >> a >> b >> c;
    if(a != 0){
        dert = pow(b, 2) - 4 * a * c;
        x1 = ((-1) * b + sqrt(dert)) / a / 2;
        x2 = ((-1) * b - sqrt(dert)) / a / 2;
        x1 > x2 ?  :(temp = x1, x1 = x2, x2 = temp);
    }
    else{
        x1 = (-1) * c / b;
        x2 = x1;
    }
    cout << fixed << setprecision(2) << x1 << ' ' << x2 << endl;
    return 0;
}


 

0.0分

1 人评分

  评论区

  • «
  • »