1297: 实验2-1-5 附加题 求解线性方程组 / Solve Ax=b

Memory Limit:128 MB Time Limit:3.000 S
Judge Style:Text Compare Creator:
Submit:58 Solved:26

Description

【中文题面】
输入一个正整数 n(1≤n≤400),再输入 n×n 系数矩阵 A 和长度为 n 的向量 b,表示线性方程组 Ax=b。
本题 OJ 只要求你选择一种方法求解并输出一个解向量:
1. 逆矩阵法:通过线性代数课上学的方法求出 A 的逆矩阵 A^{-1},再计算 x=A^{-1}b;
2. 高斯消元法:直接对增广矩阵 [A|b] 消元并回代。
你不需要、也不要在同一次提交中同时输出两种方法的结果。若方程组没有唯一解,则输出 No unique solution。若有唯一解,则输出一行解向量,所有数保留 4 位小数。
课堂要求:请先用其中一种方法提交一次;通过后,再把程序改为另一种方法并再次提交一次。两次提交的 OJ 输出格式完全相同。
本题包含较大规模测试数据,用于让两种方法的运行时间出现可观察差异。请在本地 Visual Studio 中自行构造不同规模的测试数据(例如 n=20、50、100、200、400 或更大),记录从多大规模开始两种方法的运行时间差异比较明显,并把测试方法和观察结果写入调试记录。
[English Description]
Input n (1<=n<=400), then an n x n coefficient matrix A and a vector b, representing Ax=b.
For the OJ, choose exactly one method and output one solution vector only:
1. Inverse-matrix method: compute A^{-1} by Gauss-Jordan elimination, then compute x=A^{-1}b;
2. Gaussian elimination: eliminate the augmented matrix [A|b] and perform back substitution.
Do not output both methods in one submission. If there is no unique solution, output No unique solution. Otherwise output one line of the solution vector, with all numbers printed to 4 decimal places.
Class requirement: submit once with one method, then modify the program and submit again with the other method. The output format is identical for both submissions.
Use Visual Studio locally to generate different input scales and record when the runtime difference becomes obvious.

Input

【输入】
第一行输入 n;
接下来 n 行输入矩阵 A,每行 n 个数;
最后一行输入向量 b,共 n 个数。
[Input]
The first line contains n.
The next n lines contain matrix A.
The last line contains vector b.

Output

【输出】
若无唯一解,输出 No unique solution;
否则输出一行解向量,所有数保留 4 位小数。
注意:本题只输出一种方法的结果,不输出两行。
[Output]
If there is no unique solution, output No unique solution.
Otherwise output one line of the solution vector, with all numbers printed to 4 decimal places.
Only one method's result should be printed.

Sample Input Copy

2
2 1
1 3
5 5

Sample Output Copy

2.0000 1.0000

HINT

OJ 只检查单次提交的数值输出。请分别提交“逆矩阵法版本”和“高斯消元法版本”,并在调试记录中比较两种方法的额外存储和运行时间。
建议本地构造对角占优矩阵,先指定一个已知解 x,再计算 b=A*x,这样可以方便得到任意规模的正确测试数据。
The OJ checks one output vector per submission. Submit one inverse-matrix version and one Gaussian-elimination version separately, then compare runtime and extra storage in your debug record.