题目链接
题意:计算表面积。
解法:遍历每一个数,计算4个面的表面积。
#include<cstdio>
#include<algorithm>
#include<cstring>
//#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define pb push_back
#define X first
#define Y second
#define cl(a,b) memset(a,b,sizeof(a))
typedef pair<long long ,long long > P;
const int maxn=55;
const LL inf=1LL<<45;
const LL mod=1e9+7;
int dir[][2]={1,0,0,-1,0,1,-1,0};
int a[maxn][maxn];
int main(){
int T;scanf("%d",&T);
while(T--){
int n,m;
scanf("%d%d",&n,&m);
cl(a,0);
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
scanf("%d",&a[i][j]);
}
}
int sum=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++)if(a[i][j]){
int tmp=1;
for(int k=0;k<4;k++){
int xx=i+dir[k][0];
int yy=j+dir[k][1];
if(a[i][j]>a[xx][yy]){
tmp+=a[i][j]-a[xx][yy];
}
}
sum+=tmp;
}
}
printf("%d\n",sum);
}
return 0;
}
本文介绍了一种遍历每个数并计算其四个面表面积的方法来解决计算表面积的问题。通过实例代码演示了如何使用C++实现这一算法。
&spm=1001.2101.3001.5002&articleId=49978685&d=1&t=3&u=c2fae0e311af4c5aa9e8a01c1bd29324)
3107

被折叠的 条评论
为什么被折叠?



