I Got a Matrix!【模拟】

Description–

给定一个 n ∗ m 的矩阵 A,询问位于矩阵边缘的元素之和。所谓矩阵边缘的元素,就是第一行和
最后一行的元素以及第一列和最后一列的元素。


Input–

第一行包含两个整数 n 和 m。
之后 n 行每行包含 m 个整数 Ai,j。

Output–

共一行包含一个整数 ans,表示位于矩阵边缘的元素之和。


Sample Input–

3 3
3 4 1
3 7 1
2 0 1

Sample Output–

15


说明–

对于 100% 的数据:n, m ≤ 100


代码–

#include<iostream>
#include<cstdio>
using namespace std;
int n,m,a[105][105];
long long ans;
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;++i)
	  for (int j=1;j<=m;++j)
	  {
	  	  scanf("%d",&a[i][j]);
	  	  if (i==1 || i==n || j==1 || j==m) ans+=a[i][j];
	  }
	printf("%lld",ans); 
	
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_43654542/article/details/90698728