高斯消元模板(pascal)

洛谷P3389评测

 1 program rrr(input,output);
 2 const
 3   eps=1e-8;
 4 var
 5   a:array[0..110,0..110]of double;
 6   n,i,j,k:longint;
 7   t:double;
 8 begin
 9    assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
10    readln(n);
11    for i:=1 to n do for j:=1 to n+1 do read(a[i,j]);
12    for i:=1 to n do
13       begin
14          k:=i;
15          for j:=i+1 to n do if abs(a[j,i])>abs(a[k,i]) then k:=j;
16          if abs(a[k,i])<eps then begin write('No Solution');close(input);close(output);halt; end;
17          for j:=i to n+1 do begin t:=a[i,j];a[i,j]:=a[k,j];a[k,j]:=t; end;
18          t:=a[i,i];for j:=i to n+1 do a[i,j]:=a[i,j]/t;
19          for j:=1 to n do
20             if j<>i then
21                begin
22                   t:=a[j,i];
23                   for k:=i to n+1 do a[j,k]:=a[j,k]-a[i,k]*t;
24                end;
25       end;
26    for i:=1 to n do writeln(a[i,n+1]:0:2);
27    close(input);close(output);
28 end.

 

posted @ 2017-03-08 22:04  Klaier  阅读(231)  评论(0编辑  收藏  举报