特别的代码、结果。
2026-09-20 19:27:14
发布于:广东
0阅读
0回复
0点赞
直接提交
然后按运行
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int n;
cin >> n; // 假设输入 n
int mid = n / 2; // 0-indexed center row
for (int i = 0; i < n; ++i) {
int d = abs(i - mid); // distance to center
int leftSpaces = 2 * d;
int width = 4 * mid + 1; // total width = 4*(n/2)+1 = 2n-1? Let's verify: n=7→mid=3→4*3+1=13 ✓; n=5→mid=2→4*2+1=9 ✓
// Build line: leftSpaces + '*' + middleSpaces + (second '*' if not top/bottom)
cout << string(leftSpaces, ' ');
if (d == mid) { // top or bottom row: only one '*'
cout << '*';
} else {
cout << '*';
int middleSpaces = width - 2 * leftSpaces - 2;
cout << string(middleSpaces, ' ') << '*';
}
cout << '\n';
}
}
这里空空如也

有帮助,赞一个