#include <stdio.h>
#define N 2
void solveLinearEquations(double a[N][N+1]) {
double x[N];
int i, j, k;
// ガウス消去法で方程式を解く
for (i = 0; i < N; i++) {
// ピボット選択と正規化
for (k = i + 1; k < N; k++) {
double factor = a[k][i] / a[i][i];
for (j = i; j <= N; j++) {
a[k][j] -= factor * a[i][j];
}
}
}
// 後退代入法で解を計算
for (i = N - 1; i >= 0; i--) {
x[i] = a[i][N];
for (j = i + 1; j < N; j++) {
x[i] -= a[i][j] * x[j];
}
x[i] /= a[i][i];
}
// 結果の出力
for (i = 0; i < N; i++) {
printf("x[%d] = %.2f\n", i
+ 1, x
[i
]); }
}
int main(void) {
// 連立方程式の係数行列
double a[N][N + 1] = {
{1.0, 1.0, 8.0}, // x + y = 8
{230.0, 310.0, 2080.0} // 230x + 310y = 2080
};
// 解の計算
solveLinearEquations(a);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgojZGVmaW5lIE4gMgoKdm9pZCBzb2x2ZUxpbmVhckVxdWF0aW9ucyhkb3VibGUgYVtOXVtOKzFdKSB7CiAgICBkb3VibGUgeFtOXTsKICAgIGludCBpLCBqLCBrOwogICAgCiAgICAvLyDjgqzjgqbjgrnmtojljrvms5XjgafmlrnnqIvlvI/jgpLop6PjgY8KICAgIGZvciAoaSA9IDA7IGkgPCBOOyBpKyspIHsKICAgICAgICAvLyDjg5Tjg5zjg4Pjg4jpgbjmip7jgajmraPopo/ljJYKICAgICAgICBmb3IgKGsgPSBpICsgMTsgayA8IE47IGsrKykgewogICAgICAgICAgICBkb3VibGUgZmFjdG9yID0gYVtrXVtpXSAvIGFbaV1baV07CiAgICAgICAgICAgIGZvciAoaiA9IGk7IGogPD0gTjsgaisrKSB7CiAgICAgICAgICAgICAgICBhW2tdW2pdIC09IGZhY3RvciAqIGFbaV1bal07CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CgogICAgLy8g5b6M6YCA5Luj5YWl5rOV44Gn6Kej44KS6KiI566XCiAgICBmb3IgKGkgPSBOIC0gMTsgaSA+PSAwOyBpLS0pIHsKICAgICAgICB4W2ldID0gYVtpXVtOXTsKICAgICAgICBmb3IgKGogPSBpICsgMTsgaiA8IE47IGorKykgewogICAgICAgICAgICB4W2ldIC09IGFbaV1bal0gKiB4W2pdOwogICAgICAgIH0KICAgICAgICB4W2ldIC89IGFbaV1baV07CiAgICB9CgogICAgLy8g57WQ5p6c44Gu5Ye65YqbCiAgICBmb3IgKGkgPSAwOyBpIDwgTjsgaSsrKSB7CiAgICAgICAgcHJpbnRmKCJ4WyVkXSA9ICUuMmZcbiIsIGkgKyAxLCB4W2ldKTsKICAgIH0KfQoKaW50IG1haW4odm9pZCkgewogICAgLy8g6YCj56uL5pa556iL5byP44Gu5L+C5pWw6KGM5YiXCiAgICBkb3VibGUgYVtOXVtOICsgMV0gPSB7CiAgICAgICAgezEuMCwgMS4wLCA4LjB9LCAgIC8vIHggKyB5ID0gOAogICAgICAgIHsyMzAuMCwgMzEwLjAsIDIwODAuMH0gLy8gMjMweCArIDMxMHkgPSAyMDgwCiAgICB9OwogICAgCiAgICAvLyDop6Pjga7oqIjnrpcKICAgIHNvbHZlTGluZWFyRXF1YXRpb25zKGEpOwoKICAgIHJldHVybiAwOwp9Cg==