解题思路:1、根据对称性,只看前n/2行即可
2、发现奇数行和偶数行的规律
奇数行i/2个*-,结尾i/2个-* 中间全是*
偶数行i/2个*-,结尾i/2个-* 中间全是-
参考代码:
#include <iostream>
using namespace std;
int n;
int main() {
cin >> n;
for (int i = 1; i <= n/2; i++) {
if (i % 2 == 0) {
for (int j = 0; j < i / 2; j++) {
cout << "* ";
}
for (int j = 0; j < (n - 2*i); j++) {
cout << " ";
}
for (int j = 0; j < i / 2; j++) {
cout << " *";
}
}
else {
for (int j = 0; j < i / 2; j++) {
cout << "* ";
}
for (int j = 0; j < (n - 2 * (i - 1)); j++) {
cout << "*";
}
for (int j = 0; j < i / 2; j++) {
cout << " *";
}
}
cout << endl;
}
for (int i = n / 2; i > 0; i--) {
if (i % 2 == 0) {
for (int j = 0; j < i / 2; j++) {
cout << "* ";
}
for (int j = 0; j < (n - 2 * i); j++) {
cout << " ";
}
for (int j = 0; j < i / 2; j++) {
cout << " *";
}
}
else {
for (int j = 0; j < i / 2; j++) {
cout << "* ";
}
for (int j = 0; j < (n - 2 * (i - 1)); j++) {
cout << "*";
}
for (int j = 0; j < i / 2; j++) {
cout << " *";
}
}
cout << endl;
}
return 0;
}
0.0分
0 人评分
母牛的故事 (C语言代码)浏览:479 |
C语言程序设计教程(第三版)课后习题8.3 (C语言代码)浏览:693 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:1072 |
C语言程序设计教程(第三版)课后习题8.5 (C语言代码)浏览:956 |
WU-C语言程序设计教程(第三版)课后习题11.11 (C++代码)(想学链表的可以看看)浏览:1465 |
C语言程序设计教程(第三版)课后习题8.8 (C语言代码)浏览:1482 |
字符逆序 (C语言代码)浏览:706 |
C语言训练-亲密数 (C语言代码)浏览:697 |
矩形面积交 (C++代码)浏览:1205 |
陈教主的三角形 (C语言代码)浏览:1196 |