#include<iostream>
#include<string>
using namespace std;
void test()
{
int num;
string flag;
int putout = 0;
cin >> num;
cin >> flag;
int res = 0;
int i = 1;
//找出上三角的行数
for ( i ; res < num; i++)
{
res = 2 * i * i - 1;
//cout << i << "、" << res << endl;
if (res >= num)
{
i = i - 1;
if (res == num)
{
i = i + 1;
}
break;
}
}
//cout << i << endl << endl;
//开始打印 每一行
int count = 0;//计数
int start = 2 * i - 1;//以第一次的为主//例如五
while (i > 0)
{
int temp = 0;
/* cout << 2 * i - 1 << " ";*/
count = 2 * i - 1;
//打印上三角
//打印前空格
temp = (start - count) / 2;
for (int j = 0; j < temp; j++)
{
cout << " ";
}
//打印字符
for (int j = 0; j < count; j++)
{
cout << flag;
putout++;
}
cout << endl;
i--;
}
//打印下三角
int count_1 = 0;
int k = 1;
while (count_1 < start)
{
//剔除特殊情况
if (start == 1)
{
break;
}
int temp = 0;
count_1 = 2 * k + 1;
temp = (start - count_1) / 2;
for (int j = 0; j < temp; j++)
{
cout << " ";
}
//打印字符
for (int j = 0; j < count_1; j++)
{
cout << flag;
putout++;
}
cout << endl;
k++;
}
cout << num - putout << endl;
}
int main()
{
test();
return 0;
}
【PAT-B1027】 打印沙漏 (20 分)(c++)
最新推荐文章于 2024-03-17 22:08:55 发布