3663. 打印数字菱形
//我的dd解法
#include <bits/stdc++.h>
using namespace std;
int n;
int main() {
scanf("%d", &n);
if (n == 0) {
cout << "0" << endl;
return 0;
}
int len = n * 2 - 1;
for (int i = 0; i <= n; ++i) {
for (int j = 0; j < n - i; ++j)
cout << " ";
for (int j = 0; j <= i; ++j)
cout << j << " ";
for (int j = i - 1; j >= 0; --j)
cout << j << " ";
cout << endl;
}
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < i; ++j)
cout << " ";
for (int j = 0; j <= n - i; ++j)
cout << j << " ";
for (int j = n - i - 1; j >= 0; --j)
cout << j << " ";
cout << endl;
}
return 0;
}
//y总的dl解法
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 0; i < n * 2 + 1; i++) {
for (int j = 0; j < n * 2 + 1; j++) {
int d = abs(i - n) + abs(j - n);
if (d > n)
cout << " ";
else
cout << n - d << " ";
}
cout << endl;
}
return 0;
}
题解:利用到中心的曼哈顿距离相等的点的数值都相等这一规律可以推出结果。