思路:首先需要先对打印的字符总数进行统计,计算剩下的字符数量,然后分步打印,同时需要注意打印时的格式,每一行的右侧无多余空格
代码如下:
#include<iostream>
#include<cstring>
using namespace std;
void Print(int sum,char c);
int main(){
int in_1,sign=3,sum=0;
char c_1;
cin>>in_1>>c_1;
in_1 -= 1;
while(true){
sum = sign*2;
sign += 2;
if(in_1 - sum < 0){
break;
}
in_1 = in_1 - sum;
}
Print((int)((sum/2)-2),c_1);
cout<<in_1;
return 0;
}
//打印函数
void Print(int sum,char c){
string str_1 = "";
for(int x=sum;x>1;x-=2){
cout<<str_1;
str_1 = str_1 + " ";
for(int y=0;y<x;y++){
cout<<c;
}
cout<<endl;
}
cout<<str_1<<c<<endl;
for(int z=3;z<=sum;z+=2){
str_1 = str_1.substr(0,str_1.length()-1);
cout<<str_1;
for(int w=0;w<z;w++){
cout<<c;
}
cout<<endl;
}
}
提交结果: