假算过了,请求添加hack

P2455 [SDOI2006] 线性方程组

GoldenFishX @ 2024-01-10 16:51:49

rt,评测记录

hack:

input:
4
0 0 2 1 2
0 0 0 1 1
0 0 0 0 0
0 0 1 1 1
ans:
-1
output:
0

by GoldenFishX @ 2024-01-10 16:52:22

Code

#include <bits/stdc++.h>

using namespace std;

const int MAXN = 100 + 5;

double a[MAXN][MAXN];
double eps = 1e-8;
int n;

void Swap(int x, int y) {for (int i = 0; i < n + 1; i++) swap(a[x][i], a[y][i]);}

void Find(int x) {
  int maxi = x;
  for (int i = x + 1; i < n; i++) {
    if (abs(a[i][x]) > abs(a[maxi][x])) maxi = i;
  }
  Swap(maxi, x);
}

void check1() {
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n + 1; j++) {
      cout << a[i][j] << " ";
    }
    cout << '\n';
  }
  cout << '\n';
}

int check() {
  for (int i = 0; i < n; ++i) {
    if (fabs(a[i][n]) > eps) {
      bool f = 1;
      for (int j = 0; j < n; ++j) {
        if (fabs(a[i][j]) > eps) f = 0;
      }
      if(f) return -1;
    }
  }
  return 0;
}

int main() {
  ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  bool f = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n + 1; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < n; i++) {
    Find(i);
    double xx = a[i][i];
    if (fabs(xx) < eps) {
      f = 1;
      continue;
    }
    // check1();
    for (int j = i; j < n + 1; j++) { //变成1
      a[i][j] /= xx;
    }
    for (int j = i + 1; j < n; j++) { //第i项消成0
      double x = a[j][i];
      for (int k = 0; k < n + 1; k++) {
        a[j][k] -= x * a[i][k];
      }
    }
  }
  // for (int i = n - 1; i >= 0; i--) {
  //   for (int j = 0; j < i; j++) {
  //     a[j][n] -= a[j][i] * a[i][n];
  //     a[j][i] = 0;
  //   }
  // }
  for (int j = n - 1, k = n - 1; j > -1; --j) { //j为行,k为列
    if (fabs(a[j][k]) > eps) {
      for (int i = 0; i < j; ++i) {
        a[i][n] -= a[i][k] * a[j][n];
        a[i][k] = 0;
      }
      --k;
    }
  }
  if (f) {
    cout << check() << '\n';
    return 0;
  }
  for (int i = 0; i < n; i++) {
    cout <<'x' << i + 1 << '=' << fixed << setprecision(2) << a[i][n] << '\n';
  }
  return 0;
}

by GoldenFishX @ 2024-01-10 17:01:52

@RSY


by GoldenFishX @ 2024-01-10 17:17:40

@Maxmilite


by Maxmilite @ 2024-01-10 17:28:35

@Big_Caibi Added, thx.


by _fanjin_ @ 2024-07-10 12:24:13

@GoldenFishX%%%%%%


|