题解
2026-01-18 10:04:19
发布于:河南
0阅读
0回复
0点赞
#include <iostream>
using namespace std;
int main() {
int n = 4; // 菱形“半径”:从中心到顶点的距离为3行
int totalLines = 2 * n - 1; // 总行数:7
for (int i = 0; i < totalLines; i++) {
// 计算当前行与中心的距离(绝对值)
int dist = abs(i - (n - 1));
// 前导空格数:随远离中心而减少
for (int j = 0; j < dist; j++) {
cout << " ";
}
if (dist == n - 1) {
// 顶部或底部单星号
cout << "*";
} else {
// 左星号
cout << "*";
// 中间空格数:动态计算
int spaces = 2 * (n - 1 - dist) - 1;
for (int k = 0; k < spaces; k++) {
cout << " ";
}
// 右星号
cout << "*";
}
cout << endl;
}
return 0;
}
自己看注释
这里空空如也



有帮助,赞一个