The students are currently learning systems of linear equations in two variables. Practice problems for these equations are flooding the students, and they are troubled by the repeated addition, subtraction, transposition, combining like terms, and so on. They know you are very smart and want to ask you to write a program to solve systems of linear equations in two variables for them. We assume the general format of the system of linear equations is as follows (where a, b, c, d, e, f are constants, and x, y are the unknowns): ax+by=c dx+ey=f The program reads in a, b, c, d, e, and f, then outputs the solution. Of course, the system of equations may have no solution or infinitely many solutions. If there is no corresponding real number pair (x, y) that satisfies the system of equations, then there is no solution. Conversely, if there are multiple real number pairs (x, y) that satisfy the system of equations, then there are infinitely many solutions. If there is no solution, output "No answer"; if there are infinitely many solutions, output "Too many."
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
The input consists of a single line containing six integers (a, b, c, d, e, f). The input data is guaranteed to be correct. | If there is a solution, then output on the first line "x=" followed by the value of x, and on the second line "y=" followed by the value of y, both rounded to 2 decimal places. Please refer to the sample output. If there is no solution or infinitely many solutions, output "No answer" or "Too many" as required. |
1 1 2 1 -1 0 | x=1.00 y=1.00 |
Thought Process
You can use the formulas from the sample code to solve this equation. For the output part, you can use printf("%.2f) to only output two decimal places.
Sample Code-ZeroJudge A410: Solving Equations
#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
int a1, b1, c1, a2, b2, c2;
scanf("%d%d%d%d%d%d", &a1, &b1, &c1, &a2, &b2, &c2);
int deltax1,deltax2,delta;
float x1, x2;
deltax1=c1*b2-b1*c2;
deltax2=a1*c2-c1*a2;
delta=a1*b2-a2*b1;
if(delta != 0)
{
x1=(float)deltax1/delta;
x2=(float)deltax2/delta;
printf("x=%.2f\n", x1);
printf("y=%.2f\n", x2);
}
else
{
if(deltax1==0 && deltax2==0)
cout<<"Too many"<<endl;
else
cout<<"No answer"<<endl;
}
}
//ZeroJudge A410
//Dr. SeanXD