现在的位置: 首页 > 综合 > 正文

4186. Matrix Addition

2013年05月05日 ⁄ 综合 ⁄ 共 1875字 ⁄ 字号 评论关闭

4186. Matrix Addition

Constraints

Time Limit: 1 secs, Memory Limit: 256 MB

Description

Given two n*n matrices, A and B, compute the sum C=A+B.

Input

There are multiples test cases. Each case is:

The first line is an integer n(<=10), meaning the size of the matrix. For the following 2*n lines, each line contains n integers. The first n lines contain the elements in matrix A and the following n lines contain the elements in matrix B. The absolute value of each element does not exceed 100.

Input is terminated by n=0.

Output

For each test case, output:

The sum C= A+B, there will be n lines and each line contains n integers, separating the integers by a space.

Sample Input

2

1 2

3 4

1 1

1 1

0

Sample Output

2 3

4 5

Problem Source

计算机科学与技术专业113班期中考试

// Problem#: 4186

// Submission#: 1984670

// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/

// All Copyright reserved by Informatic Lab of Sun Yat-sen University

#include<iostream>

#include<stdio.h>

#include<string>

#include<cmath>

using namespace std;

int main(){

    int n,i;

    scanf("%d",&n);

    while(n!=0){

        int x,y;

        int **a,**b;

        a=new int*[n];

        b=new int*[n];

        for(x=0;x<n;x++){

            a[x]=new int [n];

            b[x]=new int [n];

        }

        for(x=0;x<n;x++){

            for(y=0;y<n;y++){

                cin>>a[x][y];

            }

        }

        for(x=0;x<n;x++){

            for(y=0;y<n;y++){

                cin>>b[x][y];

            }

        }

        int sum=0,z=0;

        for(x=0;x<n;x++){

            for(y=0;y<n-1;y++){

                sum=(a[x][y]+b[x][y]);

                cout<<sum<<" ";

                sum=0;

            }

                sum=(a[x][y]+b[x][y]);

                cout<<sum<<endl;

                sum=0;

        }

        scanf("%d",&n);

    }

    return 0;

}                                 

抱歉!评论已关闭.