思路
打印菱形的点都是二维平面上,对于坐标分别为(x1 , y1)和(x2 , y2)的两点 p、q,它们之间的曼哈顿 距离为 | x1 - x2 | + | y1 - y2 |
菱形的“*”的点都是和 中心点(n/2,n/2)的曼哈顿距离小于等于n/2的点
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
cin >> n;
int cx = n / 2, cy = n / 2;
//定位中点的坐标
for (int i = 0; i < n; i ++ )
{
for (int j = 0; j < n; j ++ )
if (abs(i - cx) + abs(j - cy) <= n / 2) cout << '*';
//i和j是距离中点的x轴和y轴的距离
else cout << ' ';
cout << endl;
}
return 0;
}