点击打开链接 http://codeforces.com/contest/393/problem/B
Chubby Yang is studying linear equations right now. He came up with a nice problem. In the problem you are given an n × n matrix W, consisting of integers, and you should find two n × n matrices A and B, all the following conditions must hold:
题目大意:给出一个矩阵w,要求构造出两个矩阵a,b;要求a是对称矩阵a[i][j] = a[j][i],b是负对称矩阵b[i][j] = -b[j][i],并且a[i][j] + b[i][j] = w[i][j]。
解题思路:a[i][j] + b[i][j] = w[i][j]; a[j][i] + b[j][i] = a[i][j] - b[i][j] = w[j][i];两式子相加相减除2就分别是a[i][j]和b[i][j].
这道题其实很简单,可是我却A了好几遍都没过,是在没发现有什么错误,而且测试数据都对,后来发现时这样的,数组开小了,希望大家在做题的时候可以看清题意。#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n;
double a[205][205];
double flag;
double b[205][205],c[205][205];
while(cin>>n)
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
cin>>a[i][j];
if(i==j)
b[i][j]=a[i][j];
else
{
flag=(a[i][j]+a[j][i])/2;
b[i][j]=b[j][i]=flag;
c[i][j]=a[i][j]-flag;
c[j][i]=-c[i][j];
}
}
}
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(j==0)
cout<<fixed<<setprecision(8)<<b[i][j];
else
cout<<fixed<<setprecision(8)<<" "<<b[i][j];
}
cout<<endl;
}
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(j==0)
cout<<fixed<<setprecision(8)<<c[i][j];
else
cout<<fixed<<setprecision(8)<<" "<<c[i][j];
}
cout<<endl;
}
}
return 0;
}