Three matrices

点击打开链接     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:

  • Aij = Aji, for all i, j (1 ≤ i, j ≤ n);
  • Bij =  - Bji, for all i, j (1 ≤ i, j ≤ n);
  • Wij = Aij + Bij, for all i, j (1 ≤ i, j ≤ n).

Can you solve the problem?

Input

The first line contains an integer n (1 ≤ n ≤ 170). Each of the following n lines contains n integers. The j-th integer in the i-th line is Wij(0 ≤ |Wij| < 1717).

Output

The first n lines must contain matrix A. The next n lines must contain matrix B. Print the matrices in the format equal to format of matrix Win input. It is guaranteed that the answer exists. If there are multiple answers, you are allowed to print any of them.

The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 4.

Sample test(s)
input
2
1 4
3 2
output
1.00000000 3.50000000
3.50000000 2.00000000
0.00000000 0.50000000
-0.50000000 0.00000000
input
3
1 2 3
4 5 6
7 8 9
output
1.00000000 3.00000000 5.00000000
3.00000000 5.00000000 7.00000000
5.00000000 7.00000000 9.00000000
0.00000000 -1.00000000 -2.00000000
1.00000000 0.00000000 -1.00000000
2.00000000 1.00000000 0.00000000


题目大意:给出一个矩阵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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值