Problem F: Matrix Problem (II) : Array Practice

Problem F: Matrix Problem (II) : Array Practice

Time Limit: 1 Sec  Memory Limit: 4 MB
Submit: 3543  Solved: 1279
[Submit][Status][Web Board]

Description

求兩個矩陣A、B的和。根據矩陣加法的定義,只有同階的矩陣才能相加。可以確保所有運算結果都在int類型的範圍之內。

Input

輸入數據爲多個矩陣,每個矩陣以兩個正整數m和n開始,滿足0<m,n<=100,接下來爲一個m行n列的矩陣A。當輸入的m和n均爲0時,表示輸入數據結束

Output

對輸入的矩陣兩兩相加:第1個和第2個相加、第3個和第4個相加……按順序輸出矩陣相加的結果:每行兩個元素之間用一個空格分開,每行最後一個元素之後爲一個換行,在下一行開始輸出矩陣的下一行。

若輸入的矩陣不爲偶數個,最後剩餘的矩陣不產生任何輸出。

不滿足矩陣加法定義的數據輸出“Not satisfied the definition of matrix addition!”

每兩組輸出之間用一個空行分隔開。

Sample Input

3 3
1 2 3
4 5 6
7 8 9
3 3
9 8 7
6 5 4
3 2 1
3 3
1 1 1
1 1 1
1 1 1
2 2
2 2
2 2
1 1
0
0 0

Sample Output

10 10 10
10 10 10
10 10 10

Not satisfied the definition of matrix addition!

HINT

矩陣的加法就是對應位置上的元素相加。


//----------------------------------------------------------------------------------------------------上代碼[Az]

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    int arr[105][105],arra[105][105];
    int a,b;
    while(scanf("%d%d",&a,&b)!=EOF&&a!=0&&b!=0)
    {
        int i,j;
      for(i=0;i<a;i++)
     {
        for(j=0;j<b;j++)
        {
            scanf("%d",&arr[i][j]);
        }
     }
     int m,n;
     scanf("%d%d",&m,&n);
      for(i=0;i<m;i++)
     {
        for(j=0;j<n;j++)
        {
            scanf("%d",&arra[i][j]);
        }
     }
     if(m==0&&n==0)
     {
         break;
     }
     else if(m==a&&n==b)
     {
     for(i=0;i<a;i++)
     {
         for(j=0;j<b;j++)
         {
             if(!j)
             {
                 printf("%d",arr[i][j]+arra[i][j]);
             }
             else
             {
                 printf(" %d",arr[i][j]+arra[i][j]);
             }
         }
         printf("\n");
     }
     printf("\n");
     }
     else
     {
         printf("Not satisfied the definition of matrix addition!\n\n");
     }
     }
}
 
/**************************************************************
    Problem: 1054
    User: 201401061013
    Language: C
    Result: Accepted
    Time:12 ms
    Memory:764 kb
****************************************************************/


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章