o
2025-06-06 15:57:40
发布于:浙江
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int n;
cin >> n; // 输入奇数
if (n % 2 == 0) {
cout << "请输入一个奇数!" << endl;
return 0;
}
// 上半部分(包括中间行)
for (int i = 0; i <= n / 2; ++i) {
// 打印前导空格
for (int j = 0; j < n / 2 - i; ++j) {
cout << ' ';
}
// 打印菱形的边界
if (i == 0) {
cout << '*'; // 第一行只有一个星号
} else {
cout << '*' << string(2 * i - 1, ' ') << '*'; // 空心部分
}
cout << endl;
}
// 下半部分
for (int i = n / 2 - 1; i >= 0; --i) {
// 打印前导空格
for (int j = 0; j < n / 2 - i; ++j) {
cout << ' ';
}
// 打印菱形的边界
if (i == 0) {
cout << '*'; // 最后一行只有一个星号
} else {
cout << '*' << string(2 * i - 1, ' ') << '*'; // 空心部分
}
cout << endl;
}
return 0;
}
这里空空如也
有帮助,赞一个