Viết câu lệnh tính nghiệm của phương trình bậc 2 ( sử dụng cấu trúc rẽ nhánh dạng thiếu) 24/08/2021 Bởi Vivian Viết câu lệnh tính nghiệm của phương trình bậc 2 ( sử dụng cấu trúc rẽ nhánh dạng thiếu)
#include<bits/stdc++.h>using namespace std;int solve(float a, float b, float c,float &x1, float &x2){ float tdoi = b*b – 4*a*c; if(tdoi<0){ x1=x2=0.0; return 0; } else if(tdoi==0){ x1 = x2 = -b/(2*a); return 1; } else{ tdoi = sqrt(tdoi); x1 = (-b + tdoi) / (2*a); x2 = (-b – tdoi) / (2*a); return 2; }}int main(){ float a,b,c; float x1,x2; do{ cout<<“Nhap a(a!=0),b,c: “; cin>>a>>b>>c; } while(!a); int so = solve(a,b,c,x1,x2); if(so==0) { cout<<“Vo nghiem”; } else if(so==1){ cout<<“Nghiem kep x=%.4f”<<x1; } else{ cout<<“Hai nghiem phan biet:”<<“x1= “<<x1<<” x2= “<<x2; } //samon247 return 0;} Bình luận
#include<bits/stdc++.h>
using namespace std;
int solve(float a, float b, float c,float &x1, float &x2){
float tdoi = b*b – 4*a*c;
if(tdoi<0){
x1=x2=0.0;
return 0;
}
else if(tdoi==0){
x1 = x2 = -b/(2*a);
return 1;
}
else{
tdoi = sqrt(tdoi);
x1 = (-b + tdoi) / (2*a);
x2 = (-b – tdoi) / (2*a);
return 2;
}
}
int main(){
float a,b,c;
float x1,x2;
do{
cout<<“Nhap a(a!=0),b,c: “;
cin>>a>>b>>c;
}
while(!a);
int so = solve(a,b,c,x1,x2);
if(so==0) {
cout<<“Vo nghiem”;
}
else if(so==1){
cout<<“Nghiem kep x=%.4f”<<x1;
}
else{
cout<<“Hai nghiem phan biet:”<<“x1= “<<x1<<” x2= “<<x2;
}
//samon247
return 0;
}