// @BEGIN_OF_SOURCE_CODE /* @JUDGE_ID: 17243NT 190 C++ "Cramer's rule and matrices" */ // Send to judge@uva.es #include #include #include #ifdef ONLINE_JUDGE #define ins cin #define outs cout #else #define ins fin #define outs fout ifstream fin("myprog.in"); ofstream fout("myprog.out"); #endif double cmat[3][3]; double imat[3][3]; double vmat[3]; double minordet(int y, int x); double det(); void calcinv(); int main() { double x, y, c, d, e; double h, k, r; while(ins >> x >> y) { // x^2 + y^2 + Cx + Dy + E = 0 is solved for C, D, E // Make matrix corresponding to the coefficients cmat[0][0] = x; cmat[0][1] = y; cmat[0][2] = 1; vmat[0] = -(x * x + y * y); ins >> x >> y; cmat[1][0] = x; cmat[1][1] = y; cmat[1][2] = 1; vmat[1] = -(x * x + y * y); ins >> x >> y; cmat[2][0] = x; cmat[2][1] = y; cmat[2][2] = 1; vmat[2] = -(x * x + y * y); // Calculate inverse calcinv(); // Calculate C, D, E c = imat[0][0] * vmat[0] + imat[0][1] * vmat[1] + imat[0][2] * vmat[2]; d = imat[1][0] * vmat[0] + imat[1][1] * vmat[1] + imat[1][2] * vmat[2]; e = imat[2][0] * vmat[0] + imat[2][1] * vmat[1] + imat[2][2] * vmat[2]; cout.setf(ios::fixed); cout.precision(3); // Complete Squares h = c / 2; k = d / 2; r = sqrt(-e + c * c / 4 + d * d / 4); cout << "(x "; if(h < 0) cout << "- " << -h << ")^2 + (y "; else cout << "+ " << h << ")^2 + (y "; if(k < 0) cout << "- " << -k << ")^2 = " << r << "^2\n"; else cout << "+ " << k << ")^2 = " << r << "^2\n"; cout << "x^2 + y^2 "; if(c < 0) cout << "- " << -c << "x "; else cout << "+ " << c << "x "; if(d < 0) cout << "- " << -d << "y "; else cout << "+ " << d << "y "; if(e < 0) cout << "- " << -e << " = 0\n"; else cout << "+ " << e << " = 0\n"; cout << endl; } } void calcinv() { int i, j; double detc = det(); for(i = 0; i < 3; i++) for(j = 0; j < 3; j++) if((i + j) % 2 == 0) { imat[j][i] = minordet(i, j) / detc; } else { imat[j][i] = -minordet(i, j) / detc; } } double minordet(int y, int x) { int a, b; int c, d; if(y == 0) { a = 1; b = 2; } else if(y == 1) { a = 0; b = 2; } else if(y == 2) { a = 0; b = 1; } if(x == 0) { c = 1; d = 2; } else if(x == 1) { c = 0; d = 2; } else if(x == 2) { c = 0; d = 1; } return (cmat[a][c] * cmat[b][d] - cmat[a][d] * cmat[b][c]); } double det() { return ((cmat[0][0] * cmat[1][1] * cmat[2][2]) + (cmat[0][1] * cmat[1][2] * cmat[2][0]) + (cmat[0][2] * cmat[1][0] * cmat[2][1]) - (cmat[2][0] * cmat[1][1] * cmat[0][2]) - (cmat[2][1] * cmat[1][2] * cmat[0][0]) - (cmat[2][2] * cmat[1][0] * cmat[0][1])); } // @END_OF_SOURCE_CODE