Reputation: 111
On a judge platform, I encountered this problem, which I had wrong answer in 3 out of the 18 tests:
In the simultaneous equations
ax+by=c
dc+ey=f
x and y are unknowns. The coefficients a, b, c, d, e and f are integers. Each of the coefficients ranges from -2000 to 2000.
Write a program to read in the coefficients and solve for the unknowns of the corresponding simultaneous equations.
INPUT
The first and the only line contains six integers a, b, c, d, e and f separated by spaces.
OUTPUT
If there is exactly one solution, the first and the only line contains two numbers x and y, separated by a space, so that when they are substituted to the original equations, the set of simultaneous equations hold. If the set of simultaneous equations has no solution, display no solution
. If the set of simultaneous equations has more than one solution, display many solutions
.
Assume that x and y are always integers.
So, this is my code:
#include<bits/stdc++.h>
using namespace std;
int det(int a,int b,int c,int d){
return a*d-b*c;
}
int main(){
int a,b,c,d,e,f,det1,detx,dety;
cin >> a >> b >> c >> d >> e >> f;
det1=det(a,b,d,e);
detx=det(c,b,f,e);
dety=det(a,c,d,f);
if(det1==0){
if(detx==0){
cout << "many solutions";
}
else{
cout << "no solution";
}
}
else{
cout << detx/det1 << ' ' << dety/det1;
}
}
Can someone help me to debug this code?
Upvotes: 1
Views: 141
Reputation: 15265
My big guess is that I do not understand the question. A linear equation system with 2 unknown looks normally different.
If this is really the question, then we can do the following:
ax+by=c
dc+ey=f
ey = f-dc
y=(f-dc)/e
ax = c-by
= c-b((f-dc)/e)
= c-b(f-dc)/e
= c-(bf+bcd)/e
= c-bf/e+bcd/e
x = c/a-bf/(ae)+bcd/(ae)
= c/a-bf/a/e+bcd/a/e
So except with "e" or "a" being 0 there is always one deterministic solution.
That is so trivial, that I am nearly persuaded that I do not understand the question.
Even with a linear dependency of a<->dc b<->e nothing would change. Mabye
ax+by=c
dx+ey=f
was intended?
Upvotes: 2