assert(fabs(a*e - d*b) < 1.0e-8); // 錯誤:應判斷行列式非零
printf("x=%lf\ny=%lf\n", (c*e-b*f)/(a*c-d*b), (d*c-a*f)/(d*b-a*c)); // 公式錯誤
方程組:ax + by = cdx + ey = f
克拉默法則:
行列式 D = a*e - b*dDx = c*e - b*fDy = a*f - c*d
唯一解條件:|D| > epsx = Dx / D, y = Dy / D
#include <stdio.h>
#include <math.h>
#include <assert.h>
int main() {
double a,b,c,d,e,f;
scanf("%lf %lf %lf %lf %lf %lf", &a,&b,&c,&d,&e,&f);
double D = a*e - b*d;
assert(fabs(D) > 1.0e-8); // 行列式非零才有唯一解
double x = (c*e - b*f) / D;
double y = (a*f - c*d) / D;
printf("x=%lf\ny=%lf\n", x, y);
return 0;
}
輸入 5 1 8 20 4 6:D = 5*4 - 1*20 = 0 → 觸發 assert 終止程序,符合預期「解不唯一/無解」退出。