AcWing 727. 菱形
1. 地址
https://www.acwing.com/problem/content/description/729/
2. 题解
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
// 这道题需要用到曼哈顿距离
// 通过找规律发现,如果某一点跟中心点(n/2,n/2)的曼哈顿距离<=n/2的话,那么就是*,否则就是空格
// 曼哈顿距离:(x1,y1)(x2,y2) => |x2-x1| + |y2-y1|
int main(){
int n;
scanf("%d",&n);
int cx = n/2,cy = n/2;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(abs(cx-i) + abs(cy-j) <= n/2){
printf("*");
}else{
printf(" ");
}
}
printf("\n");
}
return 0;
}
标签:int,曼哈顿,727,菱形,include,AcWing
From: https://www.cnblogs.com/gao79135/p/17367821.html