bzoj1013 [ JSOI2008 ] -- 高斯消元

來源:互聯網
上載者:User

標籤:消元   lap   pac   ace   ini   jsoi2008   n+1   pre   cte   

得到n+1個方程:

(a1 1-x1)2+(a1 2-x2)2+..+(a1 n-xn)2=r2

(a2 1-x1)2+(a2 2-x2)2+..+(a2 n-xn)2=r2

...

(an+1 1-x1)2+(an+1 2-x2)2+..+(an+1 n-xn)2=r2

將後n個方程減去第一個方程就能到得到n個n個未知數的線性方程,高斯消元即可。

代碼:

 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 #define N 20 7 double a[N][N],b[N],x; 8 int i,j,k,n,m; 9 inline void Guass(){10     for(int i=1;i<=n;i++){11         double Ma=-1;int x;12         for(int j=i;j<=n;j++)if(fabs(a[j][i])>Ma)Ma=fabs(a[j][i]),x=j;13         if(x!=i)for(int j=1;j<=n+1;j++)swap(a[i][j],a[x][j]);14         double t=a[i][i];15         for(int j=1;j<=n+1;j++)a[i][j]/=t;16         for(int j=1;j<=n;j++)17         if(i!=j){18             double t=a[j][i];19             for(int k=1;k<=n+1;k++)20             a[j][k]-=t*a[i][k];21         }22     }23 }24 int main()25 {26     scanf("%d",&n);27     for(i=1;i<=n;i++)scanf("%lf",&b[i]);28     for(i=1;i<=n;i++){29         for(j=1;j<=n;j++)30         scanf("%lf",&x),a[i][j]=(x-b[j])*2,a[i][n+1]+=x*x-b[j]*b[j];31     }32     Guass();33     for(printf("%.3lf",a[1][n+1]),i=2;i<=n;i++)printf(" %.3lf",a[i][n+1]);34     return 0;35 }
bzoj1013

 

bzoj1013 [ JSOI2008 ] -- 高斯消元

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.