为什么在这个C++;它给出了两种不同的结果 下面给出了一个C++程序,求出线性方程组中x和y的值。
为什么在这个C++;它给出了两种不同的结果 下面给出了一个C++程序,求出线性方程组中x和y的值。,c++,validation,input,C++,Validation,Input,using namespace std; #include<iostream> int main() { int m,n,a,b,c,p,q,r,x,y; cout<<"For the system of equation ax+by=c and px+qy=r,"; cout<<"\nGive the value of a,b,c,p,q and r respectively:"; cin>>a>>b&
using namespace std;
#include<iostream>
int main()
{
int m,n,a,b,c,p,q,r,x,y;
cout<<"For the system of equation ax+by=c and px+qy=r,";
cout<<"\nGive the value of a,b,c,p,q and r respectively:";
cin>>a>>b>>c>>p>>q>>r;
m=q-((p*b)/a);
n=r-((p*c)/a);
if(q==0)
cout<<"No solution";
else
y=(n/m);
x=(c-(b*y))/a;
cout<<"x= "<<x<<" & y= "<<y<<"\n";
return 0;
}
使用名称空间std;
#包括
int main()
{
int m,n,a,b,c,p,q,r,x,y;
coutb>>c>>p>>q>>r;
m=q-((p*b)/a);
n=r-((p*c)/a);
如果(q==0)
cout您正在使用int
类型存储变量。在第一种情况下,a
为3,这导致方程式中的分数被截断,因为int
无法存储它们
在第二个示例中,a
为1,因此不会出现分数,程序会计算正确答案
使用float
或double