注解
1、横线的宽度跟8的下方的高度相同。8的上方跟下方高度要么相同,要么相差1,因此上方高度是upH=(h-3)/2,下方就是h-3-upH。
2、8的左边和右边的宽度就是h/6+1
3、注意行尾不能有多余空格。
代码
#include <iostream>
using namespace std;
int w, upH, downH;
char c;
int h;
void printHorizen() {
for(int i=0; i<w; i++) {
cout<<" ";
}
for(int i=0; i<downH; i++) {
cout<<c;
}
cout<<endl;
}
void printVertical(int isUp) {
int row = isUp?upH:downH;
for(int i=0; i<row; i++) {
for(int i=0; i<w; i++) {
cout<<c;
}
for(int i=0; i<downH; i++) {
cout<<" ";
}
for(int i=0; i<w; i++) {
cout<<c;
}
cout<<endl;
}
}
int main() {
int N;
cin>>N;
for(int i=0; i<N; i++) {
cin>>c>>h;
w = h/6+1;
upH = (h-3)/2;
downH = h-3-upH;
if(i){
cout<<endl;
}
printHorizen();
printVertical(1);
printHorizen();
printVertical(0);
printHorizen();
}
return 0;
}