#26744: 想問哪裡邏輯有錯


lorance1245 (lorance1245)

學校 : 不指定學校
編號 : 161188
來源 : [111.251.57.10]
最後登入時間 :
2023-02-27 21:39:05
a410. 解方程 -- TYVJ | From: [111.251.55.112] | 發表日期 : 2021-08-22 19:59

#include<iostream>

#include<iomanip>

using namespace std;

int main()

{

int a,b,c,d,e,f;

cin>>a>>b>>c>>d>>e>>f;

float x,y;

if((a*e-d*b)!=0)

{

x=(c*e-b*f)/(a*e-d*b);

y=(a*f-c*d)/(a*e-d*b);

cout<<"x="<<fixed<<setprecision(2)<<x<<endl;

cout<<"y="<<fixed<<setprecision(2)<<y<<endl;

}

else if((a*e-d*b)==0) 

{

if((c*e-b*f)==(a*f-c*d)==0)

{

cout<<"Too many"<<endl;

}

else if((c*e-b*f)==0||(a*f-c*d)==0)

{

cout<<"No answer"<<endl;

 

}

}

return 0;

}

 
#26746: Re:想問哪裡邏輯有錯


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [101.136.203.77]
最後登入時間 :
2024-04-07 15:34:14
a410. 解方程 -- TYVJ | From: [39.11.200.251] | 發表日期 : 2021-08-22 21:39

#include

#include

using namespace std;

int main()

{

int a,b,c,d,e,f;

cin>>a>>b>>c>>d>>e>>f;

float x,y;

if((a*e-d*b)!=0)

{

x=(c*e-b*f)/(a*e-d*b);

y=(a*f-c*d)/(a*e-d*b);

cout<<"x="<<fixed<<setprecision(2)<<x<<endl;

cout<<"y="<<fixed<<setprecision(2)<<y<<endl;

}

else if((a*e-d*b)==0) 

{

if((c*e-b*f)==(a*f-c*d)==0)

{

cout<<"Too many"<<endl;

}

else if((c*e-b*f)==0||(a*f-c*d)==0)

{

cout<<"No answer"<<endl;

 

}

}

return 0;

}


1. 雖然你的x和y是float,但是在C++中,int除int還是int,小數點後的數字還是沒了,你可以先轉換成float再除

2. (c*e-b*f)==(a*f-c*d)==0這裡有問題,你應該是想要判斷這兩個都是0吧?這裡應該改成(c*e-b*f)==0 && (a*f-c*d)==0

(你這種寫法是完全不同的意思,可參考https://www.includehelp.com/c/how-expression-with-multiple-comparison-evaluates-in-c-programming.aspx)

3. else if((c*e-b*f)==0||(a*f-c*d)==0)改成else

 
ZeroJudge Forum