Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://themis.lo14.wroc.pl/2ITN2015/GAUSS1
- #include <bits/stdc++.h>
- using namespace std;
- vector < vector <long double> >T;
- vector <long double> x;
- int n;
- void readMatrix() {
- long double a;
- scanf("%d ", &n);
- T.resize(0);
- x.resize(0);
- T.resize(n);
- x.resize(n);
- for (int i = 0; i < n; i++) {
- for (int j = 0; j <= n; j++) {
- scanf("%Lf", &a);
- T[i].push_back(a);
- }
- }
- }
- //void printMatrix(int a) {
- // printf("#=== %d\n", a);
- // for (int i = 0; i < n; i++) {
- // for (int j = 0; j <= n; j++) {
- // printf("%lf ", T[i][j]);
- // }
- // printf("\n");
- // }
- // printf("\n");
- //}
- void printRoots() {
- x[n - 1] = T[n - 1][n] / T[n - 1][n - 1]; //dodaje Xn
- for (int i = n - 2; i >= 0; i--) {
- long double r = T[i][n] * (-1); //to co po znaku równości
- for (int j = n - 1; j > i; j--)
- r += T[i][j] * x[j];
- x[i] = r / T[i][i];
- x[i] *= -1;
- }
- for (int i = 0; i < n; i++) {
- printf("%.6Lf ", x[i]);
- }
- printf("\n");
- }
- bool thxrafzadbg(int k) {
- for (int s = k + 1; s < n; s++) {
- if (abs(T[k][k]) < abs(T[s][k]))
- swap(T[s], T[k]);
- }
- //printMatrix(k);
- if (abs(T[k][k]) <= 0.000001) return 0;
- return 1;
- }
- void f() {
- readMatrix();
- for (int k = 0; k < n; k++) { // main loop
- ///printMatrix();
- if (!thxrafzadbg(k)) {
- printf("ZA TRUDNE\n");
- return;
- }
- for (int i = k + 1; i < n; i++) {
- long double multiplier = T[i][k] / T[k][k];
- for (int j = k; j <= n; j++) {
- T[i][j] -= T[k][j] * multiplier;
- }
- }
- }
- printRoots();
- }
- int main() {
- int testy;
- scanf("%d", &testy);
- while (testy--) f();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement