编程,求方程2X²+3X-3=0的根. 5
这是求任意一元二次方程解的代码,把它改成你那个应该很简单了
#include <iostream>
#include <cmath>
using namespace std;
int main(){
double a, b, c, x1, x2,d;
cout << "输入a,b,c"<<endl;
cin >> a >> b >> c;
d = b*b - 4 * a*c;
if (a == 0){
if (b == 0 && c == 0)
cout << "这是一个恒等式" << endl;
else if (b == 0 && c != 0)
cout << "方程无解" << endl;
else
{
x1 = -c / b;
cout << "方程只有一个解,x= " << x1 << endl;
}
}
else if (d==0)
{
x1 = -b / (2 * a);
cout << "方程有一对相同的解,x1=x2= " << x1;
}
else if (d<0)
{
cout << "方程无解" << endl;
}
else
{
x1 = (-b + sqrt(d)) / (2 * a);
x2 = (-b - sqrt(d)) / (2 * a);
cout << "方程有两个实根,x1=" << x1 << ",x2=" << x2 << endl;
}
return 0;
}
2024-11-14 广告
public static void main(String[] args) {
int a = 2;
int b = 3;
int c = -3;
double answer1,answer2;
answer1 = (-b+Math.sqrt((Math.pow(b, 2)-4*a*c)))/2*a;
answer2 = (-b+Math.sqrt((Math.pow(b, 2)-4*a*c)))/2*a;
System.out.println(answer1);
System.out.println(answer2);
}
}