AcWing 727. 菱形
原题链接
简单
作者:
SayYong
,
2024-10-02 12:23:12
,
所有人可见
,
阅读 1
规律查找
#include <iostream>
using namespace std;
int main(void)
{
int n;
cin >> n;
for (int i = 1; i <= n / 2 + 1; i++) {
for (int j = 1; j <= (n / 2) + 1 - i; j++) {
cout << ' ';
}
for (int j = 1; j <= 2 * i - 1; j++) {
cout << "*";
}
cout << endl;
}
int j = n / 2;
for (int i = 1; i <= n / 2; i++) {
for (int j = 1; j <= i; j++) {
cout << ' ';
}
for (int k = 1; k <= 2 * j - 1; k++) {
cout << '*';
}
j--;
cout << endl;
}
return 0;
}
曼哈顿距离
#include <iostream>
using namespace std;
int main(void)
{
int n;
cin >> n;
// 曼哈顿距离
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (abs(i - n / 2) + abs(j - n / 2) <= n / 2) cout << '*';
else cout << ' ';
}
cout << endl;
}
return 0;
}