#include <iostream>
#include <cmath>
using namespace std;
int main(){
int a,b,c;
while(cin>>a>>b>>c){
int i=sqrt(b*b-4*a*c);
int x1=(-b+i)/2*a;
int x2=(-b-i)/2*a;
int x3=-b/2*a;
if(i>0){
cout<<"Two different roots x1="<<x1<<" , x2="<<x2<<endl;
}
else if(i==0){
cout<<"Two same roots x="<<x3<<endl;
}
else{
cout<<"No real root"<<endl;
}
}
return 0;
}
#include
#include
using namespace std;
int main(){
int a,b,c;
while(cin>>a>>b>>c){
int i=sqrt(b*b-4*a*c);
int x1=(-b+i)/2*a;
int x2=(-b-i)/2*a;
int x3=-b/2*a;
if(i>0){
cout<<"Two different roots x1="<<x1<<" , x2="<<x2<<endl;
}
else if(i==0){
cout<<"Two same roots x="<<x3<<endl;
}
else{
cout<<"No real root"<<endl;
}
}
return 0;
}
您的答案為: Two same roots x=-4 正確答案為: Two same roots x=-1
這是系統的答案
int x1=(-b+i)/2*a;
int x2=(-b-i)/2*a;
(-b+i)/2*a
意同 ,
應寫成 (-b+i)/(2*a)
int x1=(-b+i)/2*a;
int x2=(-b-i)/2*a;
(-b+i)/2*a
意同 ,
應寫成(-b+i)/(2*a)
謝謝!!