就是找规律,但要注意一些边界,不能再输出的最前面多空格,也不能让行数多了。
#include <bits/stdc++.h>
using namespace std;
int i,j,n;
int main(){
while(cin>>n){
for(i=1;i<n;i++){
for(j=2;j<=n-i+1;j++){
cout<<" ";
}
for(j=1;j<=2*i-1;j++){
cout<<"*";
}
cout<<endl;
}
for(i=n;i>=1;i--){
for(j=2;j<=n-i+1;j++){
cout<<" ";
}
for(j=1;j<=2*i-1;j++){
cout<<"*";
}
cout<<endl;
}
}
return 0;
}