Problem E: Matrix Problem (II) : Array Practice
Time Limit: 1 Sec Memory Limit: 4 MBSubmit: 8980 Solved: 3676
[ 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 31 2 34 5 67 8 93 39 8 76 5 43 2 13 31 1 11 1 11 1 12 22 22 21 100 0
Sample Output
10 10 1010 10 1010 10 10Not satisfied the definition of matrix addition!
HINT
矩阵的加法就是对应位置上的元素相加。
Append Code//刚问的chq,freopen是不能用的,除非用预处理命令#ifndef 还有#endif,oj里面有个define叫什么judge什么的,那个就对了。
#include<stdio.h>
#include<string.h>
int main()
{
//freopen("in.txt","r",stdin);
int a[105][105],b[105][105],c,d,m,n,x,y,flag=1;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
while(scanf("%d %d",&m,&n)==2)
{
if(m==0)break;
for(c=0; c<m; c++)
{
for(d=0; d<n; d++)
{
scanf("%d",&a[c][d]);
}
}//
scanf("%d %d",&x,&y);
if(x==0)break;
for(c=0; c<x; c++)
{
for(d=0; d<y; d++)
{
scanf("%d",&b[c][d]);
}
}//
if(flag==0)printf("\n");
if(x==m&&y==n)
{
for(c=0; c<x; c++)
{
printf("%d",a[c][0]+b[c][0]);
for(d=1; d<y; d++)
{
printf(" %d",a[c][d]+b[c][d]);
}
printf("\n");
flag=0;
}//
}
else
{
printf("Not satisfied the definition of matrix addition!\n");
flag=0;
}
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
}
return 0;
}
/**************************************************************
Problem: 1054
User: 201701060928
Language: C
Result: Accepted
Time:12 ms
Memory:760 kb
****************************************************************/