AcWing 751. 数组的左方区域-3解法
原题链接
简单
作者:
枫哥
,
2024-10-24 20:25:59
,
所有人可见
,
阅读 1
#include <iostream>
#include <cstdio>
using namespace std;
// 曼哈顿距离:条件——需要找距某中心在某个距离内的坐标
int main(){
double sum = 0,count = 0,a,x = 5.5,y = 0;
char c;
cin >> c;
for (int i = 0; i < 12;i++){
for (int j = 0;j < 12;j++){
cin >> a;
if (abs(i-x)+abs(j-y) <= 4.5){
count ++;
sum +=a;
}
}
}
printf("%.1lf",c == 'S'? sum:sum/count);
return 0;
}
// 数组
int main(){
double sum = 0,count = 0;
double M[15][15];
char c;
cin >> c;
for (int i = 0; i < 12;i++){
for (int j = 0;j < 12;j++){
cin >> M[i][j];
}
}
int x = 0;
for (int i = 1;i < 11;i++){
for (int j = 0;j <= x; j++){
count++;
sum += M[i][j];
}
if (i < 5) x++;
else if (i == 5) continue;
else x--;
}
printf("%.1lf",c == 'S'? sum:sum/count);
return 0;
}
// 无数组
int main(){
double sum = 0,count = 0,a;
char c;
cin >> c;
for (int i = 0; i < 12;i++){
for (int j = 0;j < 12;j++){
cin >> a;
if (i > j && j + i < 11) {
count ++;
sum += a;
}
}
}
printf("%.1lf",c == 'S'? sum:sum/count);
return 0;
}