利用曼哈顿距离来打印菱形。
#define _CRT_SECURE_NO_WARNINGS 1标签:abs,曼哈顿,距离,int,cy,cx,printf,include From: https://blog.51cto.com/u_15815053/5890297
#include <cstdio>
#include <iostream>
int main()
{
int n;
scanf("%d", &n); //n为奇数
int cx = n / 2, cy = n / 2; //中心点的坐标
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (abs(j - cx) + abs(i - cy) <= n / 2) printf("*");
else printf(" ");
}
printf("\n");
}
return 0;
}