用C++输出
以下是使用C++编写的代码来输出该图形:
#include <iostream>
using namespace std;
int main() {
int n = 7; // 设置图形的总行数
// 输出上半部分
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < n - i - 1; j++) {
cout << "*";
}
for (int j = 0; j < 2 * i + 1; j++) {
cout << " ";
}
for (int j = 0; j < n - i - 1; j++) {
cout << "*";
}
cout << endl;
}
// 输出中间行
for (int i = 0; i < n; i++) {
cout << "*";
}
cout << endl;
// 输出下半部分
for (int i = n / 2 - 1; i >= 0; i--) {
for (int j = 0; j < n - i - 1; j++) {
cout << "*";
}
for (int j = 0; j < 2 * i + 1; j++) {
cout << " ";
}
for (int j = 0; j < n - i - 1; j++) {
cout << "*";
}
cout << endl;
}
return 0;
}
运行上述代码将输出所需的图形:
*******
*** ***
** **
* *
** **
*** ***
*******
``
原文地址: https://www.cveoy.top/t/topic/iL4D 著作权归作者所有。请勿转载和采集!