#include <iostream> #include <cmath> using namespace std; void judge(int a,int b,int c){ int D=b*b-4*a*c; int A=-b/2*a; if(D<0){ cout<<"No real root"<<endl; } else if(D==0){ cout<<"Two same roots x="<<A<<endl; } else{ int big=(-b+sqrt(D))/(2*a); int small=(-b-sqrt(D))/(2*a); cout<<"Two different roots x1="<<big<<" , x2="<<small<<endl; } } int main(){ int a,b,c; while(cin>>a>>b>>c){ judge(a,b,c); } return 0; }
#include #include using namespace std; void judge(int a,int b,int c){ int D=b*b-4*a*c; int A=-b/2*a; if(D<0){ cout<<"No real root"<<endl; } else if(D==0){ cout<<"Two same roots x="<<A<<endl; } else{ int big=(-b+sqrt(D))/(2*a); int small=(-b-sqrt(D))/(2*a); cout<<"Two different roots x1="<<big<<" , x2="<<small<<endl; } } int main(){ int a,b,c; while(cin>>a>>b>>c){ judge(a,b,c); } return 0; }
根的公式解是 -B/(2A) 你的code int A=-b/2*a; 看看哪裡怪怪的吧
測資是0所以沒驗出來
#include #include using namespace std; void judge(int a,int b,int c){ int D=b*b-4*a*c; int A=-b/2*a; if(D<0){ cout<<"No real root"<<endl; } else if(D==0){ cout<<"Two same roots x="<<A<<endl; } else{ int big=(-b+sqrt(D))/(2*a); int small=(-b-sqrt(D))/(2*a); cout<<"Two different roots x1="<<big<<" , x2="<<small<<endl; } } int main(){ int a,b,c; while(cin>>a>>b>>c){ judge(a,b,c); } return 0; }
你的int A=-b/2*a; 這行電腦再讀的時候因為沒有括號所以就直接從左到右做起,所以要改成-b/(2*a);