1 solutions

  • -1
    @ 2024-4-13 20:55:44

    #include #include #include using namespace std; #define eps 1e-7 void Read(int n = 0; bool f = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = 1; c = getchar(); } while (c >= '0' && c <= '9') { n = (n << 1) + (n << 3) + (c ^ 48); c = getchar(); } if (f) n = -n; } const int MAXN = 1e2 + 5; double a[MAXN][MAXN], ans[MAXN]; n; int main() { Read(n); for (int i = 1; i <= n; i++) for (int j = 1; j <= n + 1; j++) scanf("%lf", &a[i][j]); for (int i = 1; i <= n; i++) { int r = i; for (int j = i + 1; j <= n; j++) if (fabs(a[r][i]) < fabs(a[j][i])) r = j;//选取主元素 if (fabs(a[r][i]) < eps) return printf("No Solution"), 0;//不存在唯一解 if (i != r) swap(a[i], a[r]); double tmp = a[i][i];//带入消元 for (int j = i; j <= n + 1; j++) a[i][j] /= tmp; for (int j = i + 1; j <= n; j++) { double tmp = a[j][i]; for (int k = i; k <= n + 1; k++) a[j][k] -= a[i][k] * tmp; } } ans[n] = a[n][n + 1];//回代求解 for (int i = n - 1; i >= 1; i--) { ans[i] = a[i][n + 1]; for (int j = i + 1; j <= n; j++) ans[i] -= a[i][j] * ans[j]; } for (i <= n; i++) printf("%.2lf\n", ans[i]);

    • 1

    Information

    ID
    1695
    Time
    1000ms
    Memory
    500MiB
    Difficulty
    5
    Tags
    # Submissions
    2
    Accepted
    2
    Uploaded By