上下金字塔(菱形图形输出)
上下金字塔
找规律问题,每一行的空格数加上行号等于n,星星数等于2*行号-1(默认首行为1)
对于类似问题,先找出层数(即上半部分金字塔的层数n,下半部分就是n-1)
#include <bits/stdc++.h>
using namespace std;
signed main(){
int n;
while(cin >> n){
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n - i; ++j) cout << " ";
for(int j = 1; j <= 2 * i - 1; ++j) cout << "*";
cout << endl;
}
for(int i = n - 1; i >= 1; --i){
for(int j = n - i; j >= 1; --j) cout << " ";
for(int j = 1; j <= 2 * i - 1; ++j) cout << "*";
cout << endl;
}
}
return 0;
}

浙公网安备 33010602011771号