一共提交了6次才AC
#include <iostream>
#include<math.h>
using namespace std;
int main(){
char c;
int t;
double a;
cin>>t;
while (t--){
cin >> c;
cin >> a;
if (a > 4){
double i = ceil((a - 3) / 2); // i为下方高度 决定中间横条宽度和空格宽度
double h;
int w = (int)a/6 + 1;
if ((int)a % 2 == 0)
h = i - 1; //a为偶数上方高度
else
h = i; // a为奇数时上方的高度
for (int n = 0; n < w; n++)
cout << " ";
for (int n = 0; n < i; n++)
cout << c; //第一行打印
cout << endl;
for (int n = 0; n < h; n++){
for (int n = 0; n < w; n++)
cout << c ; //打印上方
for (int n = 0; n < i; n++)
cout << " ";
for (int n = 0; n < w; n++)
cout << c;
cout<<endl;
}
for (int n = 0; n < w; n++)
cout << " "; //打印中间横条
for (int n = 0; n < i; n++)
cout << c;
cout << endl;
for (int n = 0; n < i; n++){
for (int n = 0; n < w; n++)
cout << c ; //打印下方
for (int n = 0; n < i; n++)
cout << " ";
for (int n = 0; n < w; n++)
cout << c;
cout<< endl;
}
for (int n = 0; n < w; n++)
cout << " "; //打印最后一条
for (int n = 0; n < i; n++)
cout << c;
cout << endl;
if (t != 0)
cout << endl;
}
else
break;
}
}
多次循环同样次数用for,如果用while(t--)会因为第一次循环t的值变成0。
多注意些局部变量